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

Conversation

byteab
Copy link
Contributor

@byteab byteab commented May 22, 2021

WIP:

fixing the issue of #366

  • issue
  • test
  • code review

src/middleware
line 277:286

         if (migratedState) {
           set(migratedState)
           await setItem()
        }    
+     } else {
+        await storage.setItem(name, await serialize({...get() || {}}) 
+     }

@dai-shi dai-shi marked this pull request as draft May 22, 2021 22:02
@dai-shi
Copy link
Member

dai-shi commented May 22, 2021

Thanks for your work.
At this point, I'm not sure what issue you are trying to solve.
For #366, my suggestion is firstly to fix the async issue #366 (comment) which allows to fix the issue on lib user end, and if we need better developer experience we can follow up. (I prefer step by step.)

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

@byteab byteab closed this May 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants