Good way to update local storage on state change? #123
-
Hello, I'm looking for a good way to update local storage on state changes so they can persist between app uses. Right now I have an idea of in the store, calling the subscribe method and updating the local storage inside that callback function when the store updates. Though, from the docs it looks like subscribe only takes one value from storage? Or can I call subscribe on every value on the store without calling subscribe on each one individually? Also is there a better way to do this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Or would maybe another way be to use a update wrapper instead of subscribing? Thoughts? |
Beta Was this translation helpful? Give feedback.
-
Hi, You could just use subscribe as you described, over the entire store's state. Then whenever any value inside the store updates- the subscribe callback will be called and you can update local state with the new values. The subscribe function takes any value from your store, but that value can be calculated however you like from the entire store. You could also just return an entirely new object of just the keys and values you would like to store in local storage. |
Beta Was this translation helpful? Give feedback.
Hi,
You could just use subscribe as you described, over the entire store's state. Then whenever any value inside the store updates- the subscribe callback will be called and you can update local state with the new values. The subscribe function takes any value from your store, but that value can be calculated however you like from the entire store. You could also just return an entirely new object of just the keys and values you would like to store in local storage.