I am using Redux for state management.
How do I reset the store to its initial state?
For example, let’s say I have two user accounts (u1
and
My workaround when working with typescript, built on top of Dan's answer (redux typings make it impossible to pass undefined
to reducer as first argument, so I cache initial root state in a constant):
// store
export const store: Store = createStore(
rootReducer,
storeEnhacer,
)
export const initialRootState = {
...store.getState(),
}
// root reducer
const appReducer = combineReducers(reducers)
export const rootReducer = (state: IStoreState, action: IAction) => {
if (action.type === "USER_LOGOUT") {
return appReducer(initialRootState, action)
}
return appReducer(state, action)
}
// auth service
class Auth {
...
logout() {
store.dispatch({type: "USER_LOGOUT"})
}
}