We are storing the data for the particular component in the redux store by using creatStore() and where combining all the component's state by using combineReducers().Now Once we came out of the page we need to clear the state stored using redux. This is not the dublicate question as written in How should I clear state in componentWillUnmount? because in this question they want to clear the state of the page that they save by using this.state{}.In our scenario we have to clean from global state (redux-stored-state) for the particular component.We want a global solution so that we can apply to all our component.Please assist me.
When we refresh page in a web-app, the state always resets back to the initial values which in not a good thing when you try to build some large web-app like e-commerce. We can manually do the state persistent using the native JavaScript localStorage.
Some users prefer to keep every single piece of data in Redux, to maintain a fully serializable and controlled version of their application at all times. Others prefer to keep non-critical or UI state, such as “is this dropdown currently open”, inside a component's internal state. Using local component state is fine.
You could dispatch a reset
action in componentWillUnmount
which would be handled by a corresponding reducer. The reducer would clear the redux state.
To make it global, you might create a higher-order component that would add dispatching of the reset action to the component it's applied to. And you could have one reducer for the whole app to handle reset actions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With