How to get simple dispatch from this.props using connect w/ Redux?

后端 未结 3 552
谎友^
谎友^ 2020-11-30 16:43

I\'ve got a simple React component that I connect up (mapping a simple array/state over). To keep from referencing the context for store I\'d like a way to get \"dispatch\"

3条回答
  •  -上瘾入骨i
    2020-11-30 17:00

    You can usually mix and match based on what you'd like.

    You can pass dispatch on as a prop if that is what you want:

    export default connect(mapStateToProps, (dispatch) => ({
        ...bindActionCreators({fetchUsers}, dispatch), dispatch
    }))(Users);
    

    I'm not sure how fetchUsers is used (as an async function?), but you would usually use something like bindActionCreators to auto-bind dispatch and then you would not have to worry about using dispatch directly in connected components.

    Using dispatch directory sort of couples the dumb, stateless component with redux. Which can make it less portable.

提交回复
热议问题