Created
December 24, 2015 19:02
-
-
Save selfcontained/59881c964521b7e3ad79 to your computer and use it in GitHub Desktop.
adding dispatch fn to redux action creators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// wa la, a redux store | |
var store = createStore() | |
// some action creators | |
var actions = { | |
add: function (value) { | |
type: 'ADD', | |
value: value | |
}, | |
remove: function (id) { | |
type: 'REMOVE', | |
id: id | |
} | |
} | |
// Adds a `dispatch()` fn to each action creator | |
function addDispatch (store, actionCreators) { | |
Object.keys(actionCreators).forEach(function (actionName) { | |
var actionCreator = actionCreators[actionName] | |
// Add a dispatch function for convenience | |
actionCreator.dispatch = function () { | |
store.dispatch(actionCreator.apply(null, arguments)) | |
} | |
}) | |
return actionCreators | |
} | |
addDispatch(store, actions) | |
// dispatch right from action creators now | |
actions.add.dispatch("I still haven't see star wars...") | |
actions.remove.dispatch(1337) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment