Skip to the solution
MediumFrontend7 min read

What is hydration in Next.js and what causes a hydration error?

693 views

The problem

Question presented to candidate: "Users report a flash of different content and the console says hydration failed. What is hydration, what causes this error, and how do you fix it without hiding the bug?"

What a strong answer should cover:

  • Hydration is React attaching state and event handlers to HTML that the server already rendered, reusing the DOM instead of recreating it.
  • The first client render must produce exactly the same output as the server render; any difference is a mismatch.
  • Common causes: time and random values, typeof window branches, locale formatting, invalid HTML nesting, browser extensions changing the DOM.
  • On a mismatch React 19 discards the server HTML for that tree and re-renders on the client, which is slower and can flash.
  • Fixes: render the same value first and change it in an effect, use suppressHydrationWarning only for known cases such as timestamps, fix invalid nesting.

Clarifying questions expected:

  • "Does it happen for every user or only some?" β€” only some usually means extensions, locales or time zones.
  • "Is the mismatched value needed for the first paint?" β€” if not, render it after mount.

Code / implementation expected: Yes β€” a component that mismatches, and the corrected version.

  • nextjs
  • hydration
  • ssr
  • react

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read Β· 8 sections

Read the code

A time value that mismatches, and the fixed version
"use client";
import { useEffect, useState } from "react";

// Mismatches: the server and the browser compute different times.
export function BadClock() {
  return <p>{new Date().toLocaleTimeString()}</p>;
}

// Fixed: the first render matches the server; the real value arrives after mount.
export function Clock() {
  const [time, setTime] = useState<string | null>(null);
  useEffect(() => {
    setTime(new Date().toLocaleTimeString());
    const id = setInterval(() => setTime(new Date().toLocaleTimeString()), 1000);
    return () => clearInterval(id);
  }, []);
  return <p>{time ?? "--:--:--"}</p>;
}

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 58 of 129 in Next.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Next.js questions
Up nextMedium

What is Turbopack in Next.js and how does it compare to Webpack?

Continue