Best way to detect hydration completion
I know nuxt provides useNuxtApp().isHydrating, but what's the best way to detect when this value returns false? I've tried wrapping a component in <ClientOnly> , then adding a computed prop for isHydrating and watching that computed, but the watcher never fires even though isHydrating returns true in the onMounted hook.
What is a reliable way to detect when hydration is complete?
6 Replies
Set a global ref to false by default. Change to true when hydration completes via a client plugin utilizing the
app:suspense:resolve
hook. Use it conditionally to render components, etc. Go further by creating a onHydrated
hook combined with VueUse's watchOnce
for simplicity. For example:
That's a wonderful solution @cuebit
@cuebit Apologies if this is a stupid question but what is being passed into onHydrated (the 'cb' arg)
A callback (abbr. to cb) function that triggers when hydration completes. Useful when you want to run arbitrary logic within setup, or other composables without having to define a watch in multiple places or wrapping around client conditionals. For example:
Ah gotcha @cuebit , again thank you so much, this works beautifully and is quite elegant!
@cuebit just want to say that I love you
Now that is “Wild”. Appreciate you 👍