将更高级别的减速器与更低级别的减速器组合使用

时间:2019-03-15 13:49:45

标签: redux reducers

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

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

和两个化简器管理各自的状态:

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

我想做的就是在这两个reducer的基础上增加一个管理整个状态的reducer。我该怎么办?还是有其他更好的解决方案?

1 个答案:

答案 0 :(得分:1)

reducer只是一个需要状态和操作并返回新状态对象的函数。

combineReducers返回一个化简器,该化简器仅使用状态切片调用每个化简器。

玩这样的游戏..您可能希望它先运行组合的reducer,或者首先运行全局状态reducer,这取决于您的工作。

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

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