Skip to content

Instantly share code, notes, and snippets.

@selfcontained
Created December 24, 2015 19:02
Show Gist options
  • Save selfcontained/59881c964521b7e3ad79 to your computer and use it in GitHub Desktop.
Save selfcontained/59881c964521b7e3ad79 to your computer and use it in GitHub Desktop.
adding dispatch fn to redux action creators
// 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