HenrikJoreteg / redux-bundler

Compose a Redux store out of smaller bundles of functionality.
https://reduxbundler.com
583 stars 46 forks source link

How to 'reset' store state to initial values? #40

Closed huygn closed 5 years ago

huygn commented 5 years ago

Main use case would be right after logout - resetting all values to make sure the app is in clean state again.

HenrikJoreteg commented 5 years ago

There's a few ways to go about this... I usually clear any cached data from localStorage or IndexedDB and just refresh the app with something like window.location = '/signin'

That way I don't have to worry about accidentally leaving something in memory from a previous user.

Plus, if you have a ServiceWorker that refresh will be super fast anyway and as a user, if you hit logout, I don't think that refreshing the page is a weird or unexpected behavior.

But, if you really don't want to do that you can store initial state for reducers in a variable and add a APP_RESET case to all reducers that returns that initial state. This could be done as a function that wraps each reducer too. You could potentially also re-create the store without initial state, but... I'd suggest the first approach i mentioned. Clear caches and refresh.

huygn commented 5 years ago

hmm you're right the first approach is much simpler and works out of the box 👍 regardless I will try your 2nd suggestion and see how far I can get there, thanks.

HenrikJoreteg commented 5 years ago

I'm going to close this, but if you figure out a good solution to this, please post back to this thread, thanks!