0

I have an app written in react hooks, I manage my data with redux, we use redux-persist in order to prevent clean store on each page refresh. but, I want to clean up the store at the end of application life (close the tab or close all the browser), I tried useEffect clean-up function in App.js, but it's didn't work. where is the correct place of the end of application life?

user10391869
  • 211
  • 3
  • 4
  • 7

2 Answers2

1

If you're talking about truly closing down the application, then no "store cleanup" is needed. A Redux store is just a normal JS variable, and like all other JS variables, it will be cleaned up when the page is closed and the entire JS runtime environment is destroyed.

markerikson
  • 63,178
  • 10
  • 141
  • 157
  • The redux store not cleaned on close. (you can check it with Redux chrome extantion) – user10391869 May 04 '21 at 14:16
  • 1
    You are telling that to the author of the last version of react-redux there ;) If you do not explicitly use something like `redux-persist` to keep the state longer than that (which you do not state in the question), redux state will be lost on application close. – phry May 04 '21 at 14:20
  • you are correct, we use redux-persist, I edited the question accordingly. – user10391869 May 05 '21 at 08:07
0

Since redux-persist works by saving your state in local storage, this should most probably work.

window.onunload = () => {
     // Clear the local storage
    window.localStorage.clear();
}

Redux persist is a library allowing to save the redux store in the local storage of your browser.

localStorage.clear()

Someone Special
  • 12,479
  • 7
  • 45
  • 76