Redux:根据交叉化简器状态调度操作

Redux: dispatching an action according to cross-reducers state

本文关键字:状态 调度 操作 Redux      更新时间:2023-09-26

这是我的组合化简器:

export const element = (state, action) => {
  ...
}
export const elementSelection = (state, action) => {
  ...
}

const holoApp = combineReducers({
  elements,
  elementSelection
})
元素

选择的状态保存当前选定的元素。我希望能够根据当前选定的元素在元素Reducer中调度操作。

例如,如果store.getState().elementSelection.elementType等于"占位符",则此操作将被取消:

store.dispatch(replaceElement(...)); 

否则,将调度此操作:

store.dispatch(addElement(...)); 

唯一的猜测是将此逻辑放在由商店组成的应用程序类中,我想知道这是否是根据交叉化简器状态调度操作的最佳实践。

您可以使用 redux-thunk 在 Action Creator 中实现这一点

const someAction = elem => (dispatch, getState)=>{
   if(getState().elementSelection.elementType == "placeholder" ){
       dispatch(replaceElement(...)); 
   }else{
       dispatch(addElement(...)); 
   }
}