Binding action creators to the dispatch method
Actions creators are just functions that generate action objects which can later be used to dispatch actions using the dispatch
method. Take for example the following code:
const TYPES = { ADD_ITEM: 'ADD_ITEM', REMOVE_ITEM: 'REMOVE_ITEM', } const actions = { addItem: (name, description) => ({ type: TYPES.ADD_ITEM, payload: { name, description }, }), removeItem: (id) => ({ type: TYPES.REMOVE_ITEM, payload: { id }, }) } module.exports = actions
Later, somewhere in your application, you can dispatch these actions using the dispatch
method:
dispatch(actions.addItem('Little Box', 'Cats')) dispatch(actions.removeItem(123))
However, as you can see, calling the dispatch
method every time seems like a repeated and unnecessary step. You could simply wrap the action creators around the dispatch
function itself like this:
const actions = { addItem: (name, description) => dispatch...