Access State inside of mapDispatchToProps method

前端 未结 5 1838
清歌不尽
清歌不尽 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 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.

提交回复
热议问题