Search code examples
angulartypescriptreduxngrx-store

how to reset whole store of ngrx 9?


I already saw same quesion here but since I use ngrx9 I think there should be slighly different solution. My code and reducers in app modules look like this

.....
StoreModule.forRoot(
  reducers,
  {
    metaReducers,
    runtimeChecks: {
      strictStateImmutability: true,
      strictActionImmutability: true
    }
  }),
 ...

And my imported reducers are

export const reducers: ActionReducerMap<AppState> = {
  [friendsReducer.friendsFeatureKey]: friendsReducer.reducer,
  [authReducer.authFeatureKey]: authReducer.reducer,
  ...
};

export const metaReducers: MetaReducer<AppState>[] = !environment.production ? [] : [];

My LOGOUT action is stored in auth.actions. So how should I include root reducer to reset all the states?


Solution

  • just add metareducer to clear your state

    export function clearOnLogoutMetaReducer(reducer) {
       return function(state, action) {
          if(action.type === logout.type) {
            return reducer(undefined, action);
          }
          return reducer(state, action);
       }
    } 
    

    and add it to your metareducers array