Next.js Discord

Discord Forum

Error Hydration failed for a component with "use client"

Answered
Sun bear posted this in #help-forum
Open in Discord
Avatar
Sun bearOP
I have a list component (https://github.com/Shooshte/use-client-timestamp/blob/main/src/app/components/list.tsx) that parses unix timestamps using toLocaleTimeString. After I refresh the app in the browser I get the following error:

Unhandled Runtime Error

Hydration failed because the server rendered HTML didn't match the client. As a result this tree will be regenerated on the client. This can happen if a SSR-ed Client Component used

- A server/client branch if (typeof window !== 'undefined').
- Variable input such as Date.now() or Math.random() which changes each time it's called.
- Date formatting in a user's locale which doesn't match the server.
- External changing data without sending a snapshot of it along with the HTML.
- Invalid HTML tag nesting.

It can also happen if the client has a browser extension installed which messes with the HTML before React loaded.

See more info here: https://nextjs.org/docs/messages/react-hydration-error

I thought that putting "use client" on top of the component means that the component is always rendered in the browser, but it seems that the server is trying to hidrate my list anyway, and parses the date with a different timezone than what my browser has set?

You can find a repo with the reproduction of the issue here: https://github.com/Shooshte/use-client-timestamp

Thanks for the help, I can't figure out what is going on here.
Answered by Yi Lon Ma
use client doesn't mean your component with render only in browser
View full answer

4 Replies

Avatar
use client doesn't mean your component with render only in browser
Answer
Avatar
it will be pre rendered on the server and sent to client
you have 2 options here:
- suppress this warning
- use this component with next/dynamic and ssr:false
Avatar
Sun bearOP
Oh, thanks a lot seems that I misunderstood how this works