November 17, 2026 · Updated November 22, 2026 · 10 min read · Thomas Osayi
How to fix "Hydration failed" errors in Next.js
What a hydration mismatch is, the common causes in Next.js and React apps, and how to fix each one properly.
Hydration errors have a reputation for being cryptic, and the red overlay does not help. The cause, though, is always the same kind of thing, and once you know the five places it comes from you can usually find it in a few minutes.
React renders your page on the server, sends the HTML, then "hydrates" it in the browser by rendering again and attaching event handlers. If the two renders disagree, you get a hydration error. Everything below is about finding where they disagree and making them agree properly.
What is a hydration error in Next.js?
It is React telling you that the HTML it received from the server does not match what the same components produce on their first render in the browser.
The sequence matters. On the server, Next.js runs your components and turns the result into HTML, so the visitor sees content before any JavaScript loads. In the browser, React runs the same components again. It does not rebuild the page. It walks the existing HTML and expects each element and each piece of text to be exactly where its own render says it should be, then attaches click handlers and state.
When something differs, React cannot trust the HTML. Depending on the version and where the mismatch sits, it either patches that spot or throws away the server HTML for that part of the tree and renders it again from scratch. That costs time, can cause a visible flicker, and can leave inputs or event handlers in a broken state.
One point that surprises people: a component marked "use client" is still rendered on the server for the first load. The directive means the component also runs in the browser. It does not mean it only runs there.
What does the error message actually tell me?
It tells you which element differed and shows both versions, so read all of it before changing anything.
The wording depends on your React version:
- Recent versions: "Hydration failed because the server rendered HTML didn't match the client. As a result this tree will be regenerated on the client."
- Older versions: "Text content does not match server-rendered HTML" or "Hydration failed because the initial UI does not match what was rendered on the server."
- For bad markup: a warning that one element cannot be a descendant of another, ending with "This will cause a hydration error."
In development, the overlay includes a component stack and a diff. Lines marked with a plus are what the browser rendered, and lines marked with a minus are what the server sent. If the diff shows "10:42 AM" on one side and "6:42 PM" on the other, you have a time zone problem. If it shows an extra div, a wrapper exists on one side only.
The production build strips this detail and shows a short error code with a link. Always reproduce in development, where the message is complete.
How do I fix dates, random values and locale formatting?
Render a value that is identical on both sides first, then switch to the live or local value after the component has mounted.
Why it happens
Anything that changes between two moments or two machines will differ between the server render and the browser render:
- new Date() and Date.now(), because the two renders happen at different times
- Math.random() and generated IDs such as crypto.randomUUID()
- toLocaleDateString() and toLocaleTimeString() with no arguments, because the server's locale and time zone are not the visitor's
- Relative times such as "3 minutes ago"
This is the classic bug that only appears after deploying. Your laptop is both server and browser in development, so the time zone matches. A hosted server typically runs in UTC, and your visitor in Los Angeles is hours behind it.
The fixes
- For dates that should look the same for everyone, format with an explicit locale and time zone, for example Intl.DateTimeFormat with 'en-US' and a timeZone option. Server and browser then produce the same string.
- For dates in the visitor's local time, render a neutral value first, such as the ISO date, and replace it inside a useEffect after mount.
- For random values, generate them once on the server and pass them down as props, or generate them in an effect.
- For element IDs used to connect labels and inputs, use React's useId hook, which returns the same ID on both sides.
- Fetch the data and compute "now" in a server component, then pass the finished string to the client component, so only one render ever decides the value.
How do I use localStorage, window or screen size without a mismatch?
Read them after mount, inside an effect or through a hook built for the purpose, never during the render itself.
Browser-only data during render
The server has no window, no localStorage and no screen. Code that reads a saved theme, a cart count from localStorage, or window.innerWidth while rendering will either crash on the server or produce different output in the browser.
The common pattern in generated code is a state initializer that reads localStorage directly. The server renders the default, the browser renders the stored value, and the two disagree.
Checking typeof window does not help
Writing a condition on typeof window inside render creates two different outputs by design. The server takes one branch and the browser takes the other, which is the definition of a mismatch.
What works
- Start with the same default on both sides, then read the browser value in a useEffect and update state. The first browser render matches the server, and the real value appears right after.
- Use a mounted flag: a piece of state that starts false and is set to true in an effect, with the browser-only part rendered only when it is true.
- For values that come from a browser store, React's useSyncExternalStore hook accepts a separate server snapshot for exactly this reason.
- For layout that depends on screen size, use CSS media queries instead of JavaScript. Both layouts are in the HTML and CSS decides which is visible, so there is nothing to mismatch.
- For a widget that is entirely browser-dependent, such as a map or a chart, load it with next/dynamic and ssr set to false, from inside a client component.
The trade-off with the effect approach is a brief moment where the default shows before the stored value. For a theme, set it with a small inline script before the page paints, which is how libraries such as next-themes avoid the flash.
Which HTML nesting mistakes cause hydration errors?
Any markup the browser considers invalid, because the browser silently repairs it before React hydrates and the repaired structure no longer matches.
The frequent ones:
- A div, a list or a heading inside a p. The browser closes the paragraph early.
- A link inside a link, or a button inside a button
- A button inside a link, or a link inside a button
- Table rows placed directly in a table with no tbody, which the browser inserts for you
- A p inside a p, which usually comes from a text component that renders a paragraph being wrapped in another paragraph
- List items outside a list
These often hide inside components. A Card component renders a p for its description, and somebody passes a block of markup as the description. A rich-text field from a CMS arrives wrapped in p tags and gets placed inside another p.
Recent React versions name the elements in a warning that says one cannot be a descendant of the other. Find the outer element, and change it to a div or a span as appropriate. This is the one cause where the fix is plain: correct the markup.
Why does the error only happen for some users?
Because something on their machine changed the HTML between the server sending it and React hydrating it, most often a browser extension.
Extensions that edit pages include translators, password managers, grammar checkers, dark mode tools and some ad blockers. They add attributes to the body, inject elements, or rewrite text, and React finds a page that is not what the server sent. Recent React versions tolerate more of this than older ones, especially changes to the head and body tags, but text that has been rewritten by a translator still breaks.
To confirm, open the page in a private window with extensions off, or in a clean browser profile. If the error disappears, your code is not the cause.
Two other sources behave the same way:
- Mobile browsers that turn phone numbers and addresses into links automatically. A format-detection meta tag turns that off.
- A proxy or optimization service in front of your site that rewrites the HTML it passes through. Disable its HTML rewriting features and test again.
When is suppressHydrationWarning acceptable?
Only on a single element whose content is expected to differ, such as a timestamp, or where a library documents that it is required.
The attribute tells React not to warn about a mismatch in that one element's own text and attributes. It applies one level deep, so it does not cover the element's children. It also does not fix anything. React keeps whatever the server sent for that element.
Reasonable uses are a "last updated" time you are happy to leave as the server rendered it, and the html tag when a theme library sets a class on it before React loads. Putting it on wrappers throughout the app to silence the console is the wrong use. The mismatches are still there, and you have turned off the only thing reporting them.
How do I find the cause fast?
Read the diff, identify which of the five causes it matches, and search the named component for that pattern.
My order of checks:
- Run the app in development and reproduce the error on the exact route
- Read the diff: differing text points to dates, locale or stored values, and a differing structure points to nesting or a conditional
- Search the component and its children for Date, Math.random, toLocale, window, localStorage and typeof window
- Check the markup for the invalid nesting list above
- Test in a private window to rule out extensions
- If it only fails when deployed, compare time zones first
A worked example. An order page shows "Text content does not match" with the diff "Nov 17, 6:05 PM" against "Nov 17, 10:05 AM". The component calls toLocaleString() on the order date. The server formatted it in UTC and the browser in Pacific time. The fix is to format with an explicit time zone in a server component if every visitor should see the same time, or to render the time after mount if each visitor should see their own.
If you have worked through the list and it still fails, send me the route and the repo. A single bug like this is a Quick Fix from $60, usually within 2 days, with a write-up of the cause.
Should I turn off server rendering to make the error go away?
Only for a small widget that cannot work without the browser, never for a whole page or layout.
Loading a component with next/dynamic and ssr set to false does remove the mismatch, because the server sends nothing for that component and the browser draws it alone. Applied to a map or a chart, that is the right tool. In the App Router, that call has to sit inside a client component.
Applied to a page, it costs more than the error did:
- The HTML the server sends no longer contains that content, so search engines and AI crawlers that do not run JavaScript see an empty space
- The visitor waits for the JavaScript to download and run before anything appears, which hurts Largest Contentful Paint
- The real cause, usually a date or a localStorage read, is still in the code and will surface again somewhere else
The two-pass pattern, rendering a neutral value first and switching after mount, has a cost too. React's documentation notes that it makes hydration slower, because those components render twice. That is why I keep it to the few elements that truly differ, and leave everything else rendered once on the server.
The bottom line
A hydration error is a disagreement between two renders of the same component. Find the value or the markup that differs: a date or random value, something read from the browser during render, invalid nesting, a typeof window branch, or an extension editing the page. Make the first browser render match the server and move anything browser-specific to after mount, and the error goes away for the right reason.
Questions, answered in one line
Is a hydration error serious?
It can be: React may throw away the server HTML and re-render, which slows the page and can cause visible flicker or broken state.
Should I just add suppressHydrationWarning?
Only for a single element whose difference is expected, such as a timestamp; it hides the warning without fixing real mismatches.
Why does it only happen for some users?
Browser extensions that edit the page, such as translators or password managers, can cause mismatches on their machines only.
Why does the hydration error only appear in production?
Often because the server runs in UTC while visitors are in other time zones, so dates and times that matched on your own machine differ once deployed.
Does adding "use client" fix a hydration error?
No. Client components are still rendered on the server first, so the same mismatch happens unless the browser-only value is read after mount.
Can I turn off server rendering for one component?
Yes. Load it with next/dynamic and ssr set to false from a client component, which is appropriate for widgets that depend entirely on the browser.
Sources
Related articles
- Supabase auth redirect URL not working? Fix localhost, preview and production redirects
- Environment variables undefined on Vercel? Every cause and fix
- How to fix a CORS error on a Next.js API route
- Firestore "Missing or insufficient permissions": what triggers it and how to fix it
Fix, finish and launch your AI-built app
Built your app with Lovable, Bolt, v0, Replit or Cursor and got stuck? I fix the bugs, connect auth and payments, clean up the code and deploy it to production.
See the service