Access State inside of mapDispatchToProps method

前端 未结 5 1837
清歌不尽
清歌不尽 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:49

    Possible approach is also to use mergeProps that merges mapState and mapDispatch and allows to use both at the same time.

    // Define mapState
    const mapState = (state) => ({
      needeedValue: state.neededValue
    })
    
    // Define mapDispatch
    const mapDispatch = (dispatch, ownProps) => {
      return {
        onChange: (newValue, neededValue) => {
          dispatch(updateAttributeSelection('genre', newValue));
          dispatch(getTableData(newValue, ownProps.currentYear, neededValue));
        }
      }
    }
    
    // Merge it all (create final props to be passed)
    const mergeProps = (stateProps, dispatchProps, ownProps) => {
      return {
        ...stateProps,  // optional
        ...dispatchProps,  // optional
        onChangeWithNeededValue: (newValue) => (
          dispatchProps.onChange(
            newValue,
            stateProps.needeedValue  // <<< here the magic happens
          )
        )
      }
    }
    
    // Pass mergePros to connect
    const MyContainer = connect(mapState, mapDispatch, mergeProps)(MyComponent);
    

    Official documentation: react-redux#connect

    Possible performance drawback on larger apps: Stack Overflow - Performances and mergeProps in Redux

    0 讨论(0)
  • 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)));
        }
      };
    };
    
    0 讨论(0)
  • 2021-01-30 17:03

    If you have used Thunk Middleware then you can write helper function into your Action.Js

    export const getSearchedText =  () => (dispatch, getState) => {
        const { app } = getState();
        return app.searchedText;
    }
    

    If you have been used the container design pattern, your property container should be below

    Container.js

    export const mapDispatchToProps = (dispatch, ownProps) => {
        return {
                 setSearch: search => {
                     var searchedText = dispatch(getSearchedText());
                 }
    }
    
    0 讨论(0)
  • 2021-01-30 17:04

    You can just use redux-thunk to get state. Write a helper function like this:

    const getState = (dispatch) => new Promise((resolve) => {
      dispatch((dispatch, getState) => {resolve(getState())})
    })
    

    You can use this in a async function or generator function:

    const mapDispatchToProps = (dispatch, ownProps) => {
      return {
        async someFunction() {
          const state = await getState(dispatch)
          ...
        }
      }
    }
    
    0 讨论(0)
  • 2021-01-30 17:05

    You can use redux-thunk to create a separate action creator function which has access to getState, rather than defining the function inside mapDispatchToProps:

    function doTableActions(newValue, currentYear) {
        return (dispatch, getState) => {
            dispatch(updateAttributeSelection('genre', newValue));
            let state = getState();
            // do some logic based on state, and then:
            dispatch(getTableData(newValue, currentYear));
        }
    }
    
    
    let mapDispatchToProps = (dispatch, ownProps) => {
        return {
            onChange : (newValue) => {
                dispatch(doTableActions(newValue, ownProps.currentYear))
            }
        }
    }
    

    Some varying ways to go about organizing those, but something like that ought to work.

    0 讨论(0)
提交回复
热议问题