Skip to content
This repository was archived by the owner on Apr 18, 2023. It is now read-only.
This repository was archived by the owner on Apr 18, 2023. It is now read-only.

lazyReducerEnhancer in combination with redux-toolkit configureStore not working  #67

Description

@andreabosman16

When using the configureStore from the redux-toolkit project, the pass through of the already added reducers to the enhancer isn't working. So adding a reducer when a route is resolved, will result in a state with only the added reducer and not the previously added reducers. I wrote a fix for this problem:

export const lazyReducerEnhancer =
  (combineReducers: typeof import('redux').combineReducers,initialReducers:ReducersMapObject) => {
    const enhancer: StoreEnhancer<newStore> = (nextCreator) => (origReducer, preloadedState) => {
        const nextStore = nextCreator(origReducer, preloadedState);
        return {
          ...nextStore,
          addedReducers:{},
          addReducers(newReducers:{[index:string]:Reducer}) {
            
            Object.keys(newReducers).forEach(key=>{
              this.addedReducers[key] = newReducers[key];
            });
            
            const combinedReducerMap: ReducersMapObject = {
              ...initialReducers,
              ...this.addedReducers
            };

            this.replaceReducer(combineReducers(combinedReducerMap));
          }
        }
      }

    return enhancer;
  };

This fix would change the API of the lazyReducerEnhancer. You now need to pass the initial reducers added with the configureStore from the redux-toolkit.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions