I am using Angular 2 with ngrx/store. I want to reset the whole store states when user dispatch USER_LOGOUT
.
I read the Dan Abramov\'s answer of How to
With @ngrx/store": "^4.0.3" this is slightly different because there are small changes, so my 'clear state' looks like this
import { ActionReducerMap } from '@ngrx/store';
import { ActionReducer, MetaReducer } from '@ngrx/store';
export const rootReducer: ActionReducerMap = {
points: pointsReducer,
...
};
export function clearState(reducer: ActionReducer): ActionReducer {
return function(state: StoreStates, action: Action): StoreStates {
if (action.type === 'CLEAR_STATE') {
state = undefined;
}
return reducer(state, action);
};
}
export const metaReducers: MetaReducer[] = [clearState];
and
import { StoreModule } from '@ngrx/store';
import { metaReducers, rootReducer } from '../root.reducer';
export const imports: any = [
StoreModule.forRoot(rootReducer, { metaReducers }),
...
]