React & Next.js errors · Error 02 of 7

Next.js “Hydration failed” / text content does not match

By Muhammad Abdullah, full-stack, mobile and ai automation developer · Last reviewed

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

Short answer

The HTML rendered on the server differs from the first render in the browser. Remove browser-only or time-dependent values from the initial render, and fix invalid HTML nesting.

Common causes

  • Using Date.now(), Math.random(), or locale formatting that differs between server and browser.
  • Checking typeof window or reading localStorage during render.
  • Invalid nesting such as a <div> or <p> inside a <p>.
  • Browser extensions that modify the page before React hydrates.

How to fix it

  1. Render client-only values after mount:

                          const [now, setNow] = useState<string | null>(null);
    useEffect(() => setNow(new Date().toLocaleString()), []);
                        
  2. Fix invalid HTML nesting reported in the console.

  3. For unavoidable differences on a single element (like a timestamp), add suppressHydrationWarning to that element only.

Older React versions show this as “Text content does not match server-rendered HTML”.

Let’s talk about the next step

Have an idea, a stuck app, or a workflow toimprove?

Tell me what you are building, what is blocking you, and what a successful outcome looks like.