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 u2
).
Imagine the following sequence of events:
User
u1
logs into the app and does something, so we cache some data in the store.User
u1
logs out.User
u2
logs into the app without refreshing the browser.
At this point, the cached data will be associated with u1
, and I would like to clean it up.
How can I reset the Redux store to its initial state when the first user logs out?
With Redux if have applied the following solution, which assumes I have set an initialState in all my reducers (e.g. { user: { name, email }}). In many components I check on these nested properties, so with this fix I prevent my renders methods are broken on coupled property conditions (e.g. if state.user.email, which will throw an error user is undefined if upper mentioned solutions).
Define an action:
Then in each of your reducers assuming you are using
switch
orif-else
for handling multiple actions through each reducer. I am going to take the case for aswitch
.This way whenever you call
RESET
action, you reducer will update the store with default state.Now, for logout you can handle the like below:
Every time a userlogs in, without a browser refresh. Store will always be at default.
store.dispatch(RESET_ACTION)
just elaborates the idea. You will most likely have an action creator for the purpose. A much better way will be that you have aLOGOUT_ACTION
.Once you dispatch this
LOGOUT_ACTION
. A custom middleware can then intercept this action, either with Redux-Saga or Redux-Thunk. Both ways however, you can dispatch another action 'RESET'. This way store logout and reset will happen synchronously and your store will ready for another user login.Just a simplified answer to the best answer:
Simply have your logout link clear session and refresh the page. No additional code needed for your store. Any time you want to completely reset the state a page refresh is a simple and easily repeatable way to handle it.
I'd like to point out that the accepted comment by Dan Abramov is correct except we experienced a strange issue when using the react-router-redux package along with this approach. Our fix was to not set state to
undefined
but rather still use the current routing reducer. So I would suggest implementing the solution below if you are using this packageCombining the approaches of Dan, Ryan and Rob, to account for keeping the
router
state and initializing everything else in the state tree, I ended up with this: