bindActionCreators(actionCreators, dispatch)
#
Turns an object whose values are action creators, into an object with the same keys, but with every action creator wrapped into a dispatch
call so they may be invoked directly.
Normally you should just call dispatch
directly on your Store
instance. If you use Redux with React, react-redux will provide you with the dispatch
function so you can call it directly, too.
The only use case for bindActionCreators
is when you want to pass some action creators down to a component that isn't aware of Redux, and you don't want to pass dispatch
or the Redux store to it.
For convenience, you can also pass an action creator as the first argument, and get a dispatch wrapped function in return.
#
ParametersactionCreators
(Function or Object): An action creator, or an object whose values are action creators.dispatch
(Function): Adispatch
function available on theStore
instance.
#
Returns(Function or Object): An object mimicking the original object, but with each function immediately dispatching the action returned by the corresponding action creator. If you passed a function as actionCreators
, the return value will also be a single function.
#
ExampleTodoActionCreators.js
#
SomeComponent.js
#
#
TipsYou might ask: why don't we bind the action creators to the store instance right away, like in classical Flux? The problem is that this won't work well with universal apps that need to render on the server. Most likely you want to have a separate store instance per request so you can prepare them with different data, but binding action creators during their definition means you're stuck with a single store instance for all requests.
If you use ES5, instead of
import * as
syntax you can just passrequire('./TodoActionCreators')
tobindActionCreators
as the first argument. The only thing it cares about is that the values of theactionCreators
properties are functions. The module system doesn't matter.