Fix guides · Cursor, v0, Claude Code ·
Fix Next.js "Hydration failed" mismatch error
Error: Hydration failed because the server rendered HTML didn't match the client.
React first renders your page on the server as HTML, then re-renders it in the browser and expects both results to match exactly. This step is called "hydration". If anything differs, such as a date, a random number, a value read from localStorage, invalid HTML nesting like a <div> inside a <p>, or a browser extension changing the page, React throws this error. The fix is to make the first browser render match the server, usually by moving browser-only values into useEffect or by correcting the HTML nesting.
Common causes – and how to tell which one you have
1. Dates, times or random values rendered directly
How to tell: The mismatched text in the error overlay is a timestamp, a formatted date or number, or a random ID or value. Look for new Date(), Date.now(), Math.random() or toLocaleString() in the component's JSX. The server and the browser often run in different timezones or locales, so the same date can format differently.
Fix: Compute the value in useEffect and store it in state, so the first render shows a placeholder and the real value appears after load. You can also format the value on the server and pass it down as a fixed string. For element IDs, use React's useId() instead of Math.random().
2. Browser-only APIs read during render
How to tell: The component reads window, document, localStorage, sessionStorage, navigator or screen size while rendering, or it branches on typeof window !== 'undefined'. The server can't see any of these, so it renders something different from the browser.
Fix: Read these values inside useEffect and save them to state. If a component truly can't render on the server, load it with next/dynamic and { ssr: false } from inside a Client Component.
3. Invalid HTML nesting
How to tell: The overlay or console mentions nesting, or the mismatch involves a <p>, <a> or <button>. Common culprits are a <div>, <ul> or another <p> inside a <p>, an <a> inside an <a>, or a <button> inside a <button>. This often comes from a UI component or markdown renderer that outputs a <div> and is wrapped in a <p>.
Fix: Change the outer tag. For example, use a <div> instead of a <p> when it has block content inside, and remove the nested link or button. Browsers silently repair invalid HTML, so the browser's page structure no longer matches what React expected.
4. Browser extensions or the browser editing the page
How to tell: The error goes away in an incognito or private window with extensions off. You may also see extra attributes on <html> or <body> that you didn't add, such as ones added by Grammarly, translators, password managers or dark-mode tools. iOS Safari can also turn phone numbers and emails into links.
Fix: Your code may be fine. Confirm in a clean browser profile. If the only mismatch is extension attributes on <body> or <html>, adding suppressHydrationWarning to that single tag is acceptable.
5. Theme or dark mode set before React loads
How to tell: You use next-themes or a script that adds a class like "dark" to <html> before hydration. Or you render a sun/moon icon or theme-specific text based on the current theme.
Fix: Follow the theme library's setup, which for next-themes means adding suppressHydrationWarning to the <html> tag. For theme-dependent UI, wait until the component has mounted before rendering it.
6. Something altering the HTML in between
How to tell: The error only appears on the deployed site, not locally, or only behind a CDN. Or you use a CSS-in-JS library such as styled-components or Emotion without its Next.js setup.
Fix: Turn off CDN features that rewrite HTML, such as Cloudflare's HTML minification or email obfuscation. Configure your CSS-in-JS library as described in the Next.js CSS-in-JS docs.
How to fix it
- Open the page in dev mode and read the error overlay. It shows which element or text differs between server (-) and client (+). Note the component it points to.
- Reload in an incognito window with extensions disabled. If the error disappears, an extension is the cause, not your code.
- In the component, search for new Date(), Date.now(), Math.random(), toLocaleString(), window, document, localStorage and typeof window checks used while rendering.
- Move each browser-only or changing value into state set by useEffect. For example: const [now, setNow] = useState(null); useEffect(() => setNow(new Date().toLocaleString()), []); then render a placeholder while now is null. Use useId() for generated IDs.
- Fix any invalid nesting the console reports, such as a <div> inside a <p> or an <a> inside an <a>, by changing the outer tag.
- Reload with the console open, then run a production build (next build && next start) and confirm the error is gone in both.
Using Lovable, Bolt or v0?
In v0, paste the exact error and ask it to find components that render differently on the server and in the browser. Ask it to move date, random and localStorage logic into useEffect and to fix any <div>-inside-<p> nesting. Hydration errors happen in the browser, so check the browser's developer console rather than your Vercel deployment logs. Before changing anything, open your deployed site in an incognito window to rule out browser extensions.
Using Cursor, Claude Code or Windsurf?
Paste the full error, including the diff the Next.js dev overlay shows (the lines marked + and -). Ask the agent to name the exact component and value that differs before it edits anything. Then ask it to run next build and next start and load the affected page with the console open. In production, React shows a short minified error code instead of the full message, so check that too. Also ask it to confirm there are no nesting warnings left in the dev console.
The “fix” to avoid
Agents often scatter suppressHydrationWarning across components or add 'use client' and assume the problem is solved. suppressHydrationWarning only silences text and attribute differences one level deep. It doesn't fix structural mismatches and hides real bugs. 'use client' does nothing here, because Client Components are still rendered on the server. Wrapping whole pages in ssr: false or a "mounted" check also hides the error, but it costs you SEO and causes a blank flash, so use it only for the one widget that truly needs the browser.
Copy-paste fix prompt for your AI coding tool
Paste this into Cursor, Claude Code, Lovable or Bolt:
My Next.js app throws "Error: Hydration failed because the server rendered HTML didn't match the client." Use the dev overlay diff to identify the exact component and value that differs. Check in this order: 1) Date, Date.now(), Math.random() or toLocaleString() used during render; 2) reads of window, document, localStorage or typeof window during render; 3) invalid HTML nesting (div or p inside p, a inside a, button inside button); 4) theme classes set on <html> before hydration. Fix the cause by moving browser-only values into useState plus useEffect, using useId() for IDs, or changing the outer tag. Do not add suppressHydrationWarning broadly, and don't assume 'use client' fixes it. Explain what was mismatched, then verify by running next build && next start, loading the page with the console open, and confirming no hydration or nesting warnings remain.
Questions people ask
Does adding 'use client' fix hydration errors?
No. Client Components are still rendered to HTML on the server, so they can still mismatch. You need to remove the difference itself, for example by moving browser-only code into useEffect.
Can I just ignore the hydration error?
It's not safe to ignore. When hydration fails, React throws away the server HTML and re-renders that part of the page on the client, which slows the page and can cause flicker. The underlying mismatch can also hide real bugs.
Why does it only happen for some users or only in production?
Timezone and locale differences, browser extensions, iOS auto-linking phone numbers, and CDNs that rewrite HTML all vary by user or environment. Test in incognito, and check whether any CDN feature changes your HTML.
Official documentation
- Next.js: Text content does not match server-rendered HTML
- React: hydrateRoot – handling different client and server content