-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Persistence with createGlobalState #58
Comments
There's not a big magic behind the scenes. const initialState = {
foo: localStorage.getItem('foo') || '',
};
const container = createGlobalState(initialState);
export const setGlobalState = (key, update) => {
container.setGlobalState(key, update);
localStorage.setItem(key, container.getGlobalState(key));
};
export const useGlobalState = (key) => [
container.useGlobalState(key)[0]
(update) => setGlobalState(key, update)
]; Something like this. Function composition is the way to go. |
thank you for your response, that's such a good solution. can we add this solution to the persistance wiki page so that others can refer to it? |
If you confirm this solution works as expected, please go ahead and edit the wiki page. |
sure I will test it on my project and update the wiki. |
I understand why you ask, but it's intentionally hidden with |
v2 is released and closing this. |
Hi, thanks for your amazing work
I was wondering if there is a good way to persist state when using
createGlobalState
?I have seen the wiki, but the solution only applies to
createStore
.It would be good if we could give a callback to
createGlobalState
which it calls on every update and we could set the localstorage in there.The text was updated successfully, but these errors were encountered: