How to handle hydration on client side?
I'm using localstorage to save my local items. Component renders on server if I press F5 and throws an hydration error and it's logically, because server component doesn't have an elements from client sides and it's handled by
isServer
.
But main question: how do I handle this hydration error? How can I manage to synchronize states between server & client?1 Reply
That's how my hook looks like:
Maybe there is some way to mark only one route not to use SSR?
The only way I make it works is to
setTimeout
for getting items from localstorage, to make items synchronized between each one. But it looks like a very tricky approach to this situation and I'm sure there is more appropriate way to do this kind of stuff
tried. Not solves an issue
For now best and working way is to wrap content in onMount
in setTimeout
Such way worked for me
That's all code you need to reproduce this functionality. Here is my current build:
That's the only way I handle it works
Without timeout clientslide renders more items than serverside and hydration error appears
Not even onMount help with it
I changed it like this:
and start receive hydration errors
So this is not a solution
And the route where I'm using it:
Finally - figured out that I should use useRouteData
correct.
Docs helped
Correct way:
Yeah I have one. This is just temporary decision for this case
Yea, totally agree. But the main point was to use routeData
Which is kinda hidden in docs