这是我的组合 reducer :
export const element = (state, action) => {
...
}
export const elementSelection = (state, action) => {
...
}
const holoApp = combineReducers({
elements,
elementSelection
})
elementSelection 的状态保存当前选定的元素。我希望能够根据当前选定的元素在 elementsReducer 中调度一个操作。
例如,如果 store.getState().elementSelection.elementType 等于“placeholder”,则将调度此操作:
store.dispatch(replaceElement(...));
否则将调度此操作:
store.dispatch(addElement(...));
我唯一的猜测是将此逻辑放在由商店组成的应用程序类中,我想知道这是否是根据交叉 reducer 状态调度操作的最佳实践。
最佳答案
你可以在 Action Creator 中使用 redux-thunk 来实现这一点
const someAction = elem => (dispatch, getState)=>{
if(getState().elementSelection.elementType == "placeholder" ){
dispatch(replaceElement(...));
}else{
dispatch(addElement(...));
}
}
关于javascript - Redux:根据交叉 reducer 状态调度操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38287252/