Dates, random ids, window checks and bad HTML nesting cause most Next.js hydration failures. Here is how to spot and fix each one.
Open the component that throws and search it for four strings: window, Date, Math.random, localStorage. That finds most Next.js hydration errors before you open DevTools.
The error itself reads Hydration failed because the initial UI does not match what was rendered on the server and names no file. Here is why those four searches work, and what to do with each hit.
The model behind every case
Next.js renders your components to HTML on the server and the browser shows it. React then runs the same components on the client and compares the output with the existing DOM. Any difference throws. So each cause below is the same bug: the server and the client computed different output from the same code.
Locate the mismatch first
Do these in order and stop when you find it.
- Open the page in incognito with extensions off. If the error vanishes, an extension is involved, not your code.
- On React 19 (Next.js 15), read the diff in the dev overlay. The mismatched element is marked.
- On React 18, compare View Source (what the server sent) with the Elements panel (what the client built).
- Still lost? Comment out half the page and repeat. About four rounds finds it.
window and localStorage in the render path
const isMobile = typeof window !== 'undefined' && window.innerWidth < 768; return isMobile ? <MobileNav /> : <DesktopNav />; On the server there is no window, so you get DesktopNav. On a phone the client gets MobileNav. Mismatch. Reading localStorage in a useState initializer behaves the same way.
The standard fix is a mounted flag:
const [mounted, setMounted] = useState(false); useEffect(() => setMounted(true), []); if (!mounted) return <DesktopNav />; It works, but it costs a second render and often a layout jump. Two options are better.
If the check decides layout, use CSS media queries. Render both components and hide one with a class. No JavaScript, no mismatch.
If the value really lives in the browser (matchMedia, localStorage, online status), use useSyncExternalStore with a getServerSnapshot function. That is what the API is for, and almost nobody uses it.
My rule: a width check that decides layout belongs in CSS.
Dates and number formatting
This one shows up constantly in dashboards and blog templates.
<span>{new Date(post.createdAt).toLocaleString()}</span> The server formats the date with its own timezone and locale. Your server is probably in UTC with an en-US default. The user's browser might be UTC+5 with en-GB. Different strings, instant mismatch. Numbers and currencies through toLocaleString() break the same way.
Make the output deterministic. Pass an explicit locale and timeZone to Intl.DateTimeFormat, or send the ISO string and format it after mount. If the display must be in the user's local time, accept the two-step render and do it in useEffect. Showing a dash or a UTC date for one frame is better than an error.
Random values and ids in render
Math.random(), Date.now() and crypto.randomUUID() inside a component body give the server one value and the client another.
For form field ids and aria attributes, use useId, which produces matching ids on both sides. Don't use it for list keys. For anything else that's random, generate it in useEffect, or on the server, and pass it down as a prop.
Invalid HTML nesting
The bug isn't in your React code here. The browser's HTML parser fixes broken markup before React looks at it. Put a <div> inside a <p> and the parser closes the paragraph early, so the DOM React finds differs from the tree it expected.
Another frequent offender is a <Link> wrapping an old-style <a>, since Link renders its own anchor now. React usually prints an invalid nesting warning in the console next to the hydration error. The fix is to change the wrapper element.
Takeaway
Hydration errors are determinism bugs. Make the server and the client compute the same output and the error goes away.
In practice: reproduce in incognito, read the diff, then grep for the four strings. Decide layout in CSS. Format dates with an explicit locale and time zone. Use useId for ids. Keep suppressHydrationWarning for <html>, <body> and a single timestamp you've decided to accept, and don't reach for dynamic with ssr: false the moment an error shows up.
I left out three causes: browser extensions in detail, theme switchers with CSS-in-JS, and the production-only mismatch where something between your server and the browser rewrites the HTML. They are all in my full guide: Next.js hydration failed: 7 causes and how I fix each one.
