-
-
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): ensure argument for onRehydrateStorage
and onHydrate
is defined on first hydration
#1692
fix(middleware/persist): ensure argument for onRehydrateStorage
and onHydrate
is defined on first hydration
#1692
Conversation
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 497eb7e:
|
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.
I think it's the right fix as merge()
does the same.
Can you make changes only for newImpl
?
Done! |
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.
LGTM
onRehydrateStorage
and onHydrate
is defined on first hydrationonRehydrateStorage
and onHydrate
is defined on first hydration
[](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [zustand](https://github.com/pmndrs/zustand) | [`4.3.6` -> `4.3.9`](https://renovatebot.com/diffs/npm/zustand/4.3.6/4.3.9) | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>pmndrs/zustand (zustand)</summary> ### [`v4.3.9`](https://github.com/pmndrs/zustand/releases/tag/v4.3.9) [Compare Source](https://github.com/pmndrs/zustand/compare/v4.3.8...v4.3.9) This includes a small improvement for production build mostly for Vite users. Docs have been improved too! #### What's Changed - fix(build): mode env for "import" condition" by [@​dai-shi](https://github.com/dai-shi) in [https://github.com/pmndrs/zustand/pull/1845](https://github.com/pmndrs/zustand/pull/1845) #### New Contributors - [@​Debbl](https://github.com/Debbl) made their first contribution in [https://github.com/pmndrs/zustand/pull/1792](https://github.com/pmndrs/zustand/pull/1792) - [@​Nipodemos](https://github.com/Nipodemos) made their first contribution in [https://github.com/pmndrs/zustand/pull/1782](https://github.com/pmndrs/zustand/pull/1782) - [@​vadimshvetsov](https://github.com/vadimshvetsov) made their first contribution in [https://github.com/pmndrs/zustand/pull/1800](https://github.com/pmndrs/zustand/pull/1800) - [@​bobylito](https://github.com/bobylito) made their first contribution in [https://github.com/pmndrs/zustand/pull/1810](https://github.com/pmndrs/zustand/pull/1810) - [@​celineling](https://github.com/celineling) made their first contribution in [https://github.com/pmndrs/zustand/pull/1804](https://github.com/pmndrs/zustand/pull/1804) - [@​valerii15298](https://github.com/valerii15298) made their first contribution in [https://github.com/pmndrs/zustand/pull/1819](https://github.com/pmndrs/zustand/pull/1819) - [@​Hecmatyar](https://github.com/Hecmatyar) made their first contribution in [https://github.com/pmndrs/zustand/pull/1816](https://github.com/pmndrs/zustand/pull/1816) - [@​203x](https://github.com/203x) made their first contribution in [https://github.com/pmndrs/zustand/pull/1826](https://github.com/pmndrs/zustand/pull/1826) - [@​Ciensprog](https://github.com/Ciensprog) made their first contribution in [https://github.com/pmndrs/zustand/pull/1844](https://github.com/pmndrs/zustand/pull/1844) - [@​Romainlg29](https://github.com/Romainlg29) made their first contribution in [https://github.com/pmndrs/zustand/pull/1843](https://github.com/pmndrs/zustand/pull/1843) - [@​dcorb](https://github.com/dcorb) made their first contribution in [https://github.com/pmndrs/zustand/pull/1847](https://github.com/pmndrs/zustand/pull/1847) - [@​ivoilic](https://github.com/ivoilic) made their first contribution in [https://github.com/pmndrs/zustand/pull/1856](https://github.com/pmndrs/zustand/pull/1856) - [@​Kiku-CN](https://github.com/Kiku-CN) made their first contribution in [https://github.com/pmndrs/zustand/pull/1889](https://github.com/pmndrs/zustand/pull/1889) - [@​LartyHD](https://github.com/LartyHD) made their first contribution in [https://github.com/pmndrs/zustand/pull/1893](https://github.com/pmndrs/zustand/pull/1893) - [@​ChoiHyunjin](https://github.com/ChoiHyunjin) made their first contribution in [https://github.com/pmndrs/zustand/pull/1903](https://github.com/pmndrs/zustand/pull/1903) - [@​thesuryavivek](https://github.com/thesuryavivek) made their first contribution in [https://github.com/pmndrs/zustand/pull/1906](https://github.com/pmndrs/zustand/pull/1906) - [@​thedipankarroy](https://github.com/thedipankarroy) made their first contribution in [https://github.com/pmndrs/zustand/pull/1913](https://github.com/pmndrs/zustand/pull/1913) **Full Changelog**: pmndrs/zustand@v4.3.8...v4.3.9 ### [`v4.3.8`](https://github.com/pmndrs/zustand/releases/tag/v4.3.8) [Compare Source](https://github.com/pmndrs/zustand/compare/v4.3.7...v4.3.8) For persist middleware, a new option for createJSONStorage in introduced to support more cases. Note that createJSONStorage isn't a required function (and it's not very recommended as it's not type safe), and one should create a custom storage for more use cases. #### What's Changed - chore: add extension in imports by [@​dai-shi](https://github.com/dai-shi) in [https://github.com/pmndrs/zustand/pull/1678](https://github.com/pmndrs/zustand/pull/1678) - feat(middleware/persist): improve createJSONStorage for Maps by [@​lauhon](https://github.com/lauhon) in [https://github.com/pmndrs/zustand/pull/1763](https://github.com/pmndrs/zustand/pull/1763) - chore(tests): migrate to vitest by [@​arjunvegda](https://github.com/arjunvegda) in [https://github.com/pmndrs/zustand/pull/1753](https://github.com/pmndrs/zustand/pull/1753) #### New Contributors - [@​JacobWeisenburger](https://github.com/JacobWeisenburger) made their first contribution in [https://github.com/pmndrs/zustand/pull/1737](https://github.com/pmndrs/zustand/pull/1737) - [@​spacemeowx2](https://github.com/spacemeowx2) made their first contribution in [https://github.com/pmndrs/zustand/pull/1742](https://github.com/pmndrs/zustand/pull/1742) - [@​arjunvegda](https://github.com/arjunvegda) made their first contribution in [https://github.com/pmndrs/zustand/pull/1754](https://github.com/pmndrs/zustand/pull/1754) - [@​zc627788](https://github.com/zc627788) made their first contribution in [https://github.com/pmndrs/zustand/pull/1752](https://github.com/pmndrs/zustand/pull/1752) - [@​arvinxx](https://github.com/arvinxx) made their first contribution in [https://github.com/pmndrs/zustand/pull/1758](https://github.com/pmndrs/zustand/pull/1758) - [@​SilentFlute](https://github.com/SilentFlute) made their first contribution in [https://github.com/pmndrs/zustand/pull/1762](https://github.com/pmndrs/zustand/pull/1762) - [@​dannobytes](https://github.com/dannobytes) made their first contribution in [https://github.com/pmndrs/zustand/pull/1779](https://github.com/pmndrs/zustand/pull/1779) - [@​BLooDBRothER](https://github.com/BLooDBRothER) made their first contribution in [https://github.com/pmndrs/zustand/pull/1777](https://github.com/pmndrs/zustand/pull/1777) - [@​lauhon](https://github.com/lauhon) made their first contribution in [https://github.com/pmndrs/zustand/pull/1763](https://github.com/pmndrs/zustand/pull/1763) **Full Changelog**: pmndrs/zustand@v4.3.7...v4.3.8 ### [`v4.3.7`](https://github.com/pmndrs/zustand/releases/tag/v4.3.7) [Compare Source](https://github.com/pmndrs/zustand/compare/v4.3.6...v4.3.7) This includes a couple of improvements in `persist` middleware. #### What's Changed - feat(middleware/persist): add skip hydration option [#​405](https://github.com/pmndrs/zustand/issues/405) by [@​gmanninglive](https://github.com/gmanninglive) in [https://github.com/pmndrs/zustand/pull/1647](https://github.com/pmndrs/zustand/pull/1647) - fix(middleware/persist): ensure `persist` does not drop updates in `onRehydrateStorage` when using a synchronous storage API by [@​coffeebeats](https://github.com/coffeebeats) in [https://github.com/pmndrs/zustand/pull/1689](https://github.com/pmndrs/zustand/pull/1689) - fix(middleware/persist): ensure argument for `onRehydrateStorage` and `onHydrate` is defined on first hydration by [@​coffeebeats](https://github.com/coffeebeats) in [https://github.com/pmndrs/zustand/pull/1692](https://github.com/pmndrs/zustand/pull/1692) #### New Contributors - [@​hi-otto](https://github.com/hi-otto) made their first contribution in [https://github.com/pmndrs/zustand/pull/1687](https://github.com/pmndrs/zustand/pull/1687) - [@​pastelmind](https://github.com/pastelmind) made their first contribution in [https://github.com/pmndrs/zustand/pull/1707](https://github.com/pmndrs/zustand/pull/1707) - [@​gmanninglive](https://github.com/gmanninglive) made their first contribution in [https://github.com/pmndrs/zustand/pull/1647](https://github.com/pmndrs/zustand/pull/1647) - [@​coffeebeats](https://github.com/coffeebeats) made their first contribution in [https://github.com/pmndrs/zustand/pull/1689](https://github.com/pmndrs/zustand/pull/1689) **Full Changelog**: pmndrs/zustand@v4.3.6...v4.3.7 </details> --- ### Configuration 📅 **Schedule**: Branch creation - "after 4pm on friday,before 9am on monday,every weekend" in timezone Europe/Paris, Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/specfy/specfy). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS4xNTkuNyIsInVwZGF0ZWRJblZlciI6IjM1LjE1OS43IiwidGFyZ2V0QnJhbmNoIjoiY2hvcmUvcmVub3ZhdGVCYXNlQnJhbmNoIn0=--> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Related Issues or Discussions
Fixes #1691
Summary
This PR ensures that the argument passed to
onRehydrateStorage
andonHydrate
is defined during the first hydration of a store. Instead of passing the result ofget()
, which will always beundefined
before the first hydration,get() ?? configResult
is passed. I also added tests validating thatonRehydrateStorage
is always passed a defined value forstate
.One caveat is that it's actually not possible to add an
onHydrate
listener which will be invoked prior to first hydration. As such, I can't test this case, but I've documented it in the test I added.Alternative Solution
An alternative solution would be to update the argument type signature of
onRehydrateStorage
andonHydrate
and tostate?: S
instead ofstate: S
, which reflects the potential for state to beundefined
. This is probably not the right solution, however, as it both changes a public API and makes thepersist
API less useful.Check List
yarn run prettier
for formatting code and docs