Where to dispatch multiple actions in redux?

前端 未结 7 1584
终归单人心
终归单人心 2021-01-31 08:10

I am using redux with connect and redux-thunk middleware and containers.

Currently when an user perform an action, example one click on a butto

7条回答
  •  -上瘾入骨i
    2021-01-31 09:00

    The recommended way as per the documentation is in the action creator, like so:

    function actionCreator(payload) {
        return dispatch => {
            dispatch(action1(payload))
            dispatch(action2(payload))
        }
    }
    

    Then you would probably want to attach the action creators as prop and pass it down to the container using mapDispatchToProps like in the example mentioned here. So it would look something like so:

    const mapDispatchToProps = dispatch => ({
       action1: some_payload => dispatch(action1(some_payload))
       action2: some_payload => dispatch(action2(some_payload))
    })
    
    // your component
    export default connect(mapStateToProps, mapDispatchToProps)(YourApp)
    

提交回复
热议问题