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

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

Open
andreabosman16 opened this issue Oct 6, 2021 · 0 comments

Comments

@andreabosman16
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant