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,localStorageornavigatorduring render, so the server and browser see different values. - Invalid HTML nesting, for example a
divinside ap, 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
