Command Palette

Search for a command to run...

Hectal
Phase 20: Server Components and Next.js
React · Exercise 2 of 5 PreviousNext
Intermediate

Fix a hydration mismatch

Visits reads the visit count from localStorage while rendering. The "server" can't see localStorage, so its HTML says Welcome! while the browser's first render says Visit number 3, and hydration fails (see the console).

Fix Visits so the first client render matches the server, and the paragraph #visits shows Visit number 3 right after hydration. Keep using hydrateRoot, and don't hide the problem with suppressHydrationWarning.

What Check tests

  1. No hydration error or warning in the console
  2. #visits says Visit number 3 after hydration
  3. Still hydrates with hydrateRoot
  4. Doesn't use suppressHydrationWarning
  5. Reads storage after hydration (in an effect)

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Your page

Press Run to see the page.

Console

Nothing logged yet.