redux - Access State inside of mapDispatchToProps method -
i have written container component using redux , implementation mapdispathtoprops looks this
const mapdispatchtoprops = (dispatch, ownprops) => { return { onchange: (newvalue) => { dispatch(updateattributeselection('genre', newvalue)); dispatch(gettabledata(newvalue, ownprops.currentyear)); } } } the problem in order gettabledata need state of other components. how can access state object in method?
you can use redux-thunk create separate action creator function has access getstate, rather defining function inside mapdispatchtoprops:
function dotableactions(newvalue, currentyear) { return (dispatch, getstate) => { dispatch(updateattributeselection('genre', newvalue)); let state = getstate(); // logic based on state, , then: dispatch(gettabledata(newvalue, currentyear)); } } let mapdispatchtoprops = (dispatch, ownprops) => { return { onchange : (newvalue) => { dispatch(dotableactions(newvalue, ownprops.currentyear)) } } } some varying ways go organizing those, ought work.
Comments
Post a Comment