Search code examples
javascriptreduxngrxngrx-storengrx-store-4.0

Chaining reducers ngrx


I have feature reducers, for example: LoadReducer, DeleteReducer and CreateReducer. All of them are just simple reducers. (Also I have a higher order reducer called 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;
        };
    };
}

I want to create substate in my appstate which are using subset of my feature reducers. For example:

AppState:

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

With this when I dispatch an action {name: 'banana', type: 'Load'} it would run into bananas LoadReducer. Is there any pattern for this or any recommendation? I have tried compose and also combineReducers but with no luck. What am I missing. Is there any tool which is chaining function together.

Thanks a lot.

Regards, Tamas


Solution

  • I have found a solution in this comment: https://github.com/reactjs/redux/issues/1920#issuecomment-243921970