Reputation: 267260
I have multiple reducers which I combine together in my redux application.
const reducers = combineReducers({
reducer1,
reducer2,
reducer3
});
When a user performs a logout, how do I delete my entire redux state for this user?
Upvotes: 3
Views: 6516
Reputation: 2889
The answer is simple create an action lets call it "CLEARSTORE".
In the reducer handle this action by returning an empty object.
export function CLEARSTORE(){
return {
type:"CLEARSTORE"
};
}
reducer.js
const appReducer = combineReducers({
/* your app’s top-level reducers */
})
const rootReducer = (state, action) => {
if (action.type === 'CLEARSTORE') {
state = undefined
}
return appReducer(state, action)
}
Upvotes: 4
Reputation: 2522
Multiple reducers can handle the same action just fine in Redux.
It's completely ok and actually very convenient as you might want to have different part of the state tree to react to the event in different way. In the situation of logout, each reducers can return different default value of the state tree that it takes care of.
What I normally handle logout is to dispatch an action, for example: RESET_STATE
, and have all the reducers react to it.
You might want to have a look on how Redux author said about handling same action among multiple reducers.
https://github.com/reduxjs/redux/issues/601
Upvotes: 0
Reputation: 5669
When you click logout, dispatch a simple action like "CLEAR_ALL" and this can be added in the switch cases of all reducers and from there an empty state or the initial state can be returned. Thus clearing all values in store.
Upvotes: 6