链式减速机ngrx

时间:2018-03-29 07:27:52

标签: javascript redux ngrx ngrx-store ngrx-store-4.0

我有功能缩减器,例如:LoadReducerDeleteReducerCreateReducer。所有这些都只是简单的减速器。 (我还有一个名为namedReducer的更高级的减速器

export function namedReducer(reducerName: string) {
    return (reducer: Function) => {
        return function newReducer(state, action) {
            const { name } = action;
            const isInitializationCall = state === undefined;
            const shouldRunWrappedReducer = reducerName === name || isInitializationCall;
            return shouldRunWrappedReducer ? reducer(_.get(state, reducerName), action) : state;
        };
    };
}

我想在我的appstate中创建使用我的功能减少器子集的子状态。例如:

届时AppState:

{
  bananaState: namedReducer('banana')(/* here chaining together CreateReducer and LoadReducer */),
  appleState: namedReducer('apple')(/* here chaining together LoadReducer, DeleteReducer */) 
}

当我发送一个动作{name: 'banana', type: 'Load'}时,它会遇到香蕉LoadReducer。这个或任何建议有什么模式吗?我尝试了composecombineReducers,但没有运气。我错过了什么是否有任何链接功能的工具。

非常感谢。

此致 的Tamas

1 个答案:

答案 0 :(得分:0)

我在此评论中找到了一个解决方案:https://github.com/reactjs/redux/issues/1920#issuecomment-243921970