When would bindActionCreators be used in react/redux?

后端 未结 10 1084
温柔的废话
温柔的废话 2020-12-07 11:04

Redux docs for bindActionCreators states that:

The only use case for bindActionCreators is when you want to pass some action cre

10条回答
  •  孤城傲影
    2020-12-07 11:34

    99% of the time, it's used with the React-Redux connect() function, as part of the mapDispatchToProps parameter. It can be used explicitly inside the mapDispatch function you provide, or automatically if you use the object shorthand syntax and pass an object full of action creators to connect.

    The idea is that by pre-binding the action creators, the component you pass to connect() technically "doesn't know" that it's connected - it just knows that it needs to run this.props.someCallback(). On the other hand, if you didn't bind action creators, and called this.props.dispatch(someActionCreator()), now the component "knows" that it's connected because it's expecting props.dispatch to exist.

    I wrote some thoughts on this topic in my blog post Idiomatic Redux: Why use action creators?.

提交回复
热议问题