Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ export const persist = <S extends State>(
} else {
set(deserializedStorageValue.state)
}
} else {
await storage.setItem(name, await serialize({...get() || {}})
Copy link
Member

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.

Copy link
Contributor Author

@byteab byteab May 23, 2021

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

Copy link
Member

@dai-shi dai-shi May 23, 2021

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.

Copy link
Member

@dai-shi dai-shi May 23, 2021

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.)

Copy link
Contributor Author

@byteab byteab May 23, 2021

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.

}
} catch (e) {
postRehydrationCallback?.(undefined, e)
Expand Down