bindActionCreators(actionCreators, dispatch)

    Normally you should just call dispatch directly on your instance. If you use Redux with React, react-redux will provide you with the 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.

    Parameters

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

    • dispatch (Function): A function available on the Store instance.

    Returns

    Example

    TodoActionCreators.js

    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 must dispatch the action, too!
    31. // This will work:
    32. let action = TodoActionCreators.addTodo('Use Redux')
    33. dispatch(action)
    34. }
    35. render() {
    36. // Injected by react-redux:
    37. let { todos } = this.props
    38. return <TodoList todos={todos} {...this.boundActionCreators} />
    39. // An alternative to bindActionCreators is to pass
    40. // just the dispatch function down, but then your child component
    41. // needs to import action creators and know about them.
    42. // return <TodoList todos={todos} dispatch={dispatch} />
    43. }
    44. }
    45. export default connect(state => ({ todos: state.todos }))(TodoListContainer)

    Tips

    • 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.