Access State inside of mapDispatchToProps method

前端 未结 5 1851
清歌不尽
清歌不尽 2021-01-30 16:37

I have written a container component using redux and my implementation for mapDispatchToProps looks like this

const mapDispatchToProps = (dispatch, o         


        
5条回答
  •  逝去的感伤
    2021-01-30 16:57

    You can try to use:

    redux-named-reducers

    Which allows you to get state anywhere in your code like so:

    const localState1 = getState(reducerA.state1)
    const localState2 = getState(reducerB.state2)
    

    Likewise in mapDispatchToProps:

    const mapDispatchToProps = dispatch => {
      return {
        onClick: () => {
          dispatch(someAction(getState(moduleA.state1)));
        }
      };
    };
    

提交回复
热议问题