lostpebble / pullstate

Simple state stores using immer and React hooks - re-use parts of your state by pulling it anywhere you like!
https://lostpebble.github.io/pullstate
MIT License
1.08k stars 23 forks source link

How to Reset all the stores? #159

Closed SteJ13 closed 1 year ago

SteJ13 commented 2 years ago

Discussed in https://github.com/lostpebble/pullstate/discussions/158

Originally posted by **SteJ13** September 29, 2022 I am new to pullstate and I liked it very much. It is very useful and best alternate for redux. Thanks to the community. Since I am new to this package I don't know how to reset all the stores. Eg. I have around 5 files with 5 seperate stores. When I logout the user and login as a new user the old data is still in the store which I don't need them. I know I can update the initial state but for many stores it may be difficult. Can any one of you guide me to overcome this issue?
lostpebble commented 2 years ago

Hi,

I would recommend creating a function called getDefaultStoreState() or something along those lines.

You can use this function to fill the store when you initialize it for the first time. And then you can also call Store.replace(getDefaultStoreState()) when you need it to be reset. Stores are individually managed and not connected in any way, so you'd have to do this for each store.

Store state management is entirely up to you, and there are multiple ways to deal with these kinds of issues.

In the future it might make sense for a Store.reset() method.

Hope that helps you!