How do you fix a hydration error in Next.js?

← Next.js answers

Make the first client render produce exactly the HTML the server sent. Move anything that differs (time, randomness, browser-only data) into useEffect.

React compares the HTML from the server with what the client renders first. Any difference triggers a hydration warning. The usual causes are:

  • Values that change between renders, such as Date.now(), Math.random() or a locale-dependent date format.
  • Reading window, localStorage or navigator during render, so the server and browser see different values.
  • Invalid HTML nesting, for example a div inside a p, which the browser silently rewrites.
  • Browser extensions that modify the page before React loads.
"use client";
import { useEffect, useState } from "react";
 
export function LocalTime({ iso }: { iso: string }) {
  const [text, setText] = useState(iso); // same on server and first client render
 
  useEffect(() => {
    setText(new Date(iso).toLocaleString()); // runs only in the browser
  }, [iso]);
 
  return <time dateTime={iso}>{text}</time>;
}

For a single element where a mismatch is expected and harmless, such as a timestamp, suppressHydrationWarning on that element silences the warning for it.

Hydration problems often depend on a user's browser and extensions. Session replay shows what the page looked like when it broke. Learn more

Related questions

Start debugging smarter today

Try Bugsee for free — no commitments, no hidden fees.

Free sign upLive demoNo credit card required