onFinishHydration doesn't fire on render #3138
-
Bug DescriptionI have a store that is using persist. In my project, I need to trigger an event automatically when the store hydrates because that event relies on the data in the store. In the context However, the event does not fire on the initial hydrate when the page loads. If using code like Manually triggering a rehydration using Reproduction Linkhttps://stackblitz.com/edit/stackblitz-starters-u8yakq1m?file=app%2Fpage.tsx |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 10 replies
-
I guess, before you call |
Beta Was this translation helpful? Give feedback.
-
Ah, thats a good point. I dug around in the docs for a bit and added a log to the hydration itself in the persist and it shows that the hydration is already finished by the time the listener is added. For whatever reason, react still makes the initial render without the hydrated data but that is probably just a react thing, not a zustand thing. Something to do with server rendering I think but honestly I don't know. Thanks! |
Beta Was this translation helpful? Give feedback.
-
Would it be considered a breaking change to have the event listener called immediately on registration if the store is already hydrated? |
Beta Was this translation helpful? Give feedback.
I guess, before you call
persist.onFinishHyrdration()
, the initialhydrate()
is already finished. So usingpersist.rehydrate()
sounds correct for your use case.