Skip to solution
mediumFrontend

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

692 views
01

Understand the problem

Attaching React to server HTML and why mismatches break it.

nextjshydrationssrreact
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Hydration is when React attaches event handlers to the server-rendered HTML on the client. A hydration error happens when the client render differs from the server HTML — e.g. using Date.now(), random values, or browser-only APIs during render — so the trees don't match.

Solution ready — 2 min read

Classified // press E to declassify

04

Read the code

Avoiding a hydration mismatch
'use client';
import { useState, useEffect } from 'react';

export default function Clock() {
  const [time, setTime] = useState<string | null>(null);
  // ✅ compute browser-only value AFTER mount, not during render
  useEffect(() => setTime(new Date().toLocaleTimeString()), []);
  return <span suppressHydrationWarning>{time ?? 'loading…'}</span>;
}
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 42 of 95 decoded in the Next.js track. One more won't hurt.

Back to track