-
-
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(middleware/persist): Merge storage value with the latest store state #894
Conversation
When using zustand store with Provider and Context, it's possible to mutate the store state in an useEffect callback before hydration creates `stateFromStorage`. It should merge the storage value with the latest store state instead of the config result since it can be out of date.
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 0134814:
|
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.
Thanks for the PR, but unfortunately this change breaks quite a few tests
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.
Looks good, thanks!
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.
Thanks for your contribution!
When using persist middleware, it's possible to mutate the store state in an useEffect callback before hydration creates
stateFromStorage
. It should merge the storage value with the latest store state instead of the config result since it can be out of date.To reproduce:
Expected: When the storage value is fetched, it merges storage value with the latest store value.
Actual: When the storage value is fetched, it merges storage value with the initial store value.
Error demo: https://codesandbox.io/s/react-forked-oo16v5?file=/src/App.js
Fixed demo: https://codesandbox.io/s/react-forked-hydp10?file=/src/App.js