Where to dispatch multiple actions in redux?

前端 未结 7 1581
终归单人心
终归单人心 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条回答
  •  鱼传尺愫
    2021-01-31 09:05

    While solution by @GibboK did not work for me:

    const mapDispatchToProps = (dispatch) => ({
      action2: id => dispatch(Actions.action2(id)),
      action3: id => dispatch(Actions.action3(id)),
      action1: (dateId, attrId) => {
        return dispatch => {
          dispatch(Actions.action2(dateId));
          dispatch(Actions.action3(attrId));
        }
      }
    });
    

    I eventually went with redux-batched-actions. Worked like charm:

    const mapDispatchToProps = (dispatch) => ({
      action2: id => dispatch(Actions.action2(id)),
      action3: id => dispatch(Actions.action3(id)),
      action1: (dateId, attrId) =>
        dispatch(batchActions([
          Actions.action2(dateId),
          Actions.action3(attrId)
        ]))
    });
    

提交回复
热议问题