Skip to content
>_VibeUnstuck

Hydration failed because the server rendered HTML didn't match the client.

Last verified:

The fix

Hydration failed because the server rendered HTML didn't match the client. Render one tree first. Read the clock, a random value, or localStorage inside useEffect, after that first paint.

  1. 1

    Paint a stable value, then fill in the browser value

    The server and the first client render both show the same string. useEffect runs only in the browser, so the locale time appears after hydration. Do the same for localStorage, Math.random(), and window.innerWidth.

    "use client";
    
    import { useEffect, useState } from "react";
    
    export function LocalTime({ iso }: { iso: string }) {
      const [label, setLabel] = useState(iso);
      useEffect(() => {
        setLabel(new Date(iso).toLocaleString());
      }, [iso]);
      return <time dateTime={iso}>{label}</time>;
    }
  2. 2

    Read the diff and stop at that component

    The console names the branch that differed. A typeof window check that returns different JSX is this error. Make both sides return the same elements, then update state in an effect.

  3. 3

    Fix the nesting the browser rewrites

    A p cannot contain a div, another p, or a heading. The browser closes the paragraph early, so the tree React hydrates is not the tree it rendered. Move the block out of the paragraph.

  4. 4

    Reproduce an attribute diff with extensions off

    If the diff is an attribute you did not write, open a private window with extensions disabled. When the warning disappears there, leave the component alone. suppressHydrationWarning belongs on that one element, never on html or body to quiet the whole app.

Why it happens

Next.js sends HTML from the server, then React attaches event handlers to that HTML. The first client render has to describe the same tree. React's note on this error names the usual sources: a typeof window !== 'undefined' branch, Date.now() or Math.random(), a date formatted in the visitor's locale, and invalid nesting such as a div inside a p.

A file that starts with "use client" still renders on the server once, and that pass has no window. suppressHydrationWarning silences one element, one level deep, and React does not patch the mismatched text. It is for a timestamp you have decided to live with. A password manager or a translator can also inject an attribute before React loads. That diff is not in your component.

The next error you'll probably hit

Once this is fixed, the usual follow-up is ReferenceError: window is not defined. window is not defined: Next.js read window, document, or localStorage on the server. Move that read into useEffect, or load the widget with ssr: false.

Prompt to paste back into your AI tool

Paste this above the error.

Hydration failed because the server rendered HTML didn't match the client. The diff points at this component:
<paste the component and the diff>

Do not add suppressHydrationWarning on html or body. Do not set ssr: false on the whole page. Do not add "use client" and keep a typeof window branch that returns different JSX.

1. Name the value that differs (time, random, locale, localStorage, window, or invalid HTML nesting).
2. Make the first render identical. Move the browser-only read into useEffect, as in a component that renders the ISO string first and calls toLocaleString after mount.
3. If the markup is a div or heading inside a p, move the block out.
4. If the diff is an attribute the component does not set, say so and tell me to retry with extensions disabled. Change only this component.

Common questions

+Does "use client" stop Hydration failed because the server rendered HTML didn't match the client?

No. Client Components are still pre-rendered on the server. The first client render has to match that HTML. The directive only lets you use state and effects.

+Is suppressHydrationWarning the fix?

Only for one element's text or attribute, one level deep, such as a timestamp you accept. React will not repair a different tree. Do not put it on html or body.

+The diff says Text content does not match server-rendered HTML. Is that the same error?

Yes. That sentence is the text-node form. A date, a locale string, or Math.random() during render is the usual source. The attribute form says a tree hydrated but some attributes didn't match. An extension is the usual source of an attribute you did not write.

+Why does it only happen in production?

Development re-renders and shows the diff. A production page still hydrates the HTML from the build. A value that was different at build time, such as Date.now() in a Server Component's output, is frozen in that HTML.

Related errors