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.

Parameters

  • actionCreators (Function or Object): An action creator, or an object whose values are action creators.

  • dispatch (Function): A dispatch function available on the Store 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.

Example

TodoActionCreators.js

  1. export function addTodo(text) {
  2. return {
  3. type: 'ADD_TODO',
  4. text
  5. }
  6. }
  7. export function removeTodo(id) {
  8. return {
  9. type: 'REMOVE_TODO',
  10. id
  11. }
  12. }

SomeComponent.js

  1. import { Component } from 'react'
  2. import { bindActionCreators } from 'redux'
  3. import { connect } from 'react-redux'
  4. import * as TodoActionCreators from './TodoActionCreators'
  5. console.log(TodoActionCreators)
  6. // {
  7. // addTodo: Function,
  8. // removeTodo: Function
  9. // }
  10. class TodoListContainer extends Component {
  11. constructor(props) {
  12. super(props)
  13. const { dispatch } = props
  14. // Here's a good use case for bindActionCreators:
  15. // You want a child component to be completely unaware of Redux.
  16. // We create bound versions of these functions now so we can
  17. // pass them down to our child later.
  18. this.boundActionCreators = bindActionCreators(TodoActionCreators, dispatch)
  19. console.log(this.boundActionCreators)
  20. // {
  21. // addTodo: Function,
  22. // removeTodo: Function
  23. // }
  24. }
  25. componentDidMount() {
  26. // Injected by react-redux:
  27. let { dispatch } = this.props
  28. // Note: this won't work:
  29. // TodoActionCreators.addTodo('Use Redux')
  30. // You're just calling a function that creates an action.
  31. // You must dispatch the action, too!
  32. // This will work:
  33. let action = TodoActionCreators.addTodo('Use Redux')
  34. dispatch(action)
  35. }
  36. render() {
  37. // Injected by react-redux:
  38. let { todos } = this.props
  39. return <TodoList todos={todos} {...this.boundActionCreators} />
  40. // An alternative to bindActionCreators is to pass
  41. // just the dispatch function down, but then your child component
  42. // needs to import action creators and know about them.
  43. // return <TodoList todos={todos} dispatch={dispatch} />
  44. }
  45. }
  46. export default connect(state => ({ todos: state.todos }))(TodoListContainer)

Tips

  • You 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 pass require('./TodoActionCreators') to bindActionCreators as the first argument. The only thing it cares about is that the values of the actionCreators properties are functions. The module system doesn't matter.