How to reset the state of a Redux store?
To reset your Redux store, dispatch a specific RESET
action. Handle this in your reducers to return to initial state:
Delegate to appReducer
Implement a rootReducer
which really is an appReducer
handling the flow of resetting for you:
The rootReducer
sets the state
to undefined
when a RESET
action is dispatched and delegates to appReducer
. This triggers individual reducers to reset to their initial state.
Persistent state reset with redux-persist
For a persistent Redux state across page reloads, redux-persist
comes to the rescue:
Invoking resetAndCleanStore
will cleanse your persistent store and reset the state upon user logout.
Taming specific reducers
Don't want a complete reset? Handle the RESET
action in specific reducers:
Async action handling for logout
For asynchronous operations, consider using middleware such as Redux-Saga or Redux-Thunk along with your RESET
handling. Here's how it works with Redux-Thunk:
With middleware, the reset process is decoupled from user actions and can handle async cleanup logic.
Supercharge with Redux Toolkit
Redux Toolkit (RTK), especially with TypeScript, enhances store setup and state resets. Take a look:
Experience predictable and type-safe state management with RTK case reducers.
Was this article helpful?