Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear the component stored redux state in componentWillUnmount?

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.

like image 789
Gorakh Nath Avatar asked Apr 20 '17 05:04

Gorakh Nath


People also ask

Does Redux state reset on refresh?

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.

Do you need to Keepis all component states in Redux store?

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.


1 Answers

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.

like image 65
m1kael Avatar answered Oct 19 '22 05:10

m1kael