-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
fix issue of persisting data on initial load. #396
Conversation
Thanks for your work. |
@@ -281,6 +281,8 @@ export const persist = <S extends State>( | |||
} else { | |||
set(deserializedStorageValue.state) | |||
} | |||
} else { | |||
await storage.setItem(name, await serialize({...get() || {}}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems another issue from what I think we should fix firstly, but I am not sure if this is desired. At least I don't want to write to localStorage without change. I think this should be optional, and if async issues are solved, there's a workaround, and we will see what we want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the initial value of the store shouldn't be written in localStorage? Do you mean this?
for example
const useStore = create((set) => ({
friends: 10,
setFriends: () => set({friends: 20})
}, {name: "my-storage"}))
so the localStorage will not store the value friends: 10
, until you update the value by setFriends
here is a sandbox that replicate it:
https://codesandbox.io/s/zustand-persist-debug-0k6ui?file=/src/App.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, in my use case in mind, I wouldn't store the default value.
const useStore = create(persist((set) => ({
userName: "No Name",
setUserName: (userName) => set({ userName })
}), { name: "my-user-setting" }))
In this case, I don't want to store "No Name" in localStorage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, and your use case of storing default value seems already covered by postRehydrationCallback
. So, it's already supported, I'd say.
https://codesandbox.io/s/zustand-persist-debug-forked-bi4nr?file=/src/App.js
Not super pretty, but it works.
(The issue it doesn't show on the screen is another issue, which I refer as the first issue.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
storing default value is optional. and if someone wants to store, it should use the postRehydrationCallback
callback.
Ok, I got it.
WIP:
fixing the issue of #366
src/middleware
line 277:286