将高级减速器与组合的低级减速器一起使用

Using higher level reducer alongside combined lower level reducers

我有一个 react/redux 应用,其商店设置如下:

interface AppState {
  substateA: SubstateA;
  substateB: SubstateB;
}

还有两个管理各自状态的减速器:

const reducer = combineReducers<AppState>({
    substateA: reducerA,
    substateb: reducerB,
});

我想做的是在这两个减速器之外添加一个管理整个状态的减速器。我该怎么做或者有其他更好的解决方案吗?

reducer 只是一个接受状态和动作的函数,returns 一个新的状态对象。

combineReducers returns 一个 reducer,它只调用每个 reducer 的状态片段。

试一试这样的东西。您可能希望它首先 运行 组合减速器,或者首先是您的全局状态减速器,具体取决于您在做什么。

const combinedReducer = combineReducers<AppState>({
  substateA: reducerA,
  substateb: reducerB,
});

const reducer = (state, action) => {
  state = reducerC(state, action);
  return combinedReducer(state, action);
}