Polymer / pwa-helpers

Small helper methods or mixins to help you build web apps.
BSD 3-Clause "New" or "Revised" License
440 stars 48 forks source link

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

Open andreabosman16 opened 3 years ago

andreabosman16 commented 3 years ago

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.