-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make sure ActionCreators will work with es6 arrow syntax. Fixes #83 #84
Make sure ActionCreators will work with es6 arrow syntax. Fixes #83 #84
Conversation
💄 ES6 arrow functions are bound to the current scope making it impossible to call `this.dispatch(...)` or other methods of the ActionCreators inside the callback. By passing context, one can use `context.dispatch(...)` or other methods of the ActionCreator. Since context is passed as the last argument, all existing code work as is and if you don't use es6 arrow syntax, you're not affected.
Fixes #83 |
Damn, I knew messing with the function context would come back to bite me! Arrow operators are technically not necessary however I understand that people are going to use them for syntactic sugar. The only function that is actually unique is
|
Nice catch. Passing And also, I think not extending function actionContext() {
return {
dispatch: function () {
return dispatch({
type: actionType,
handlers: handlers,
arguments: arguments
}, properties);
}
};
} That will pass all tests, but am not so sure it won't break some code out there. Suggestions? |
Good stuff.
|
I'm using webpack with es6-loader and the above code will be transpiled to: var UserActionCreators = Marty.createActionCreators({
addUser: UserActions.ADD_USER(function(name, email, dispatch) {
dispatch(name, email);
this.somethingElse(); // Fails since this != UserActionCreators here, use UserActionCreators.somethingElse() instead
}.bind(this)),
somethingElse: UserActions.SOMETHING_ELSE()
}); But from an es5 point of view, it makes perfect sense. I will update the docs for es6 users to explicitly use the global Thanks. |
This does feel a bit like a misuse of the syntax. Fat arrow function's are designed to ensure that the function context is set to the current context. It's terseness is just a side effect. I really don't see what value fat arrow functions bring here except removing 6 characters. I'm wondering if its just better to advise against using them here? |
… es6 arrow syntax for ActionCreators.
That works for me. I updated the docs. |
Make sure ActionCreators will work with es6 arrow syntax. Fixes #83
👍 thanks :) |
💄
In some es6 transpilers, ES6 arrow functions are bound to the current scope making it impossible to call
this.dispatch(...)
or other methods of the ActionCreators inside the callback.By passing context as the last argument to the ActionCreator, one can use
context.dispatch(...)
or other methods of the ActionCreator.Since context is passed as the last argument, all existing code work as is and if you don't use es6 arrow syntax, you're not affected.