Skip to content
FunnelGuard

Fix guide by FunnelsGuard

React/Next hydration mismatch error: how to find and fix it

PRF-119Medium impactDetectedBy FunnelGuardPublished Updated
On this page

What this issue is

Frameworks like Next.js build your page as HTML on the server. In the browser, React then "hydrates" that HTML: it attaches event handlers to the existing markup so buttons and forms work. To do that, React expects its first browser render to match the server HTML exactly.

When the two differ, React reports a hydration mismatch. Typical console messages are Hydration failed because the initial UI does not match what was rendered on the server, Text content does not match server-rendered HTML and There was an error while hydrating. In development you see an overlay. In production the message is shorter, often a minified error such as #418, #423 or #425.

Why it costs you leads or sales

A mismatch is often cosmetic, but on a funnel page it can do real damage. A form may flash and lose what a visitor already typed. A button may not respond until the client re-render finishes, or may not respond at all if hydration gives up. Layout can jump under the visitor's finger. Scripts that run on hydration, such as tracking events, may fire late, twice or not at all.

How a hydration mismatch hurts a lead form
  1. Server sends HTMLPage looks fine and loads fast
  2. React hydratesBrowser output differs from server HTML
  3. Form or button respondsDead, reset or delayed
  4. Lead and tracking hitNever sent or sent late

The break happens at hydration; everything after it depends on the page working.

How to check if your site has it

Check for hydration errors
  1. Open the page in a private window

    Use a fresh window with no extensions so browser add-ons do not alter the HTML.

  2. Open DevTools and reload

    Press F12, open the Console tab, tick Preserve log, and reload. Look for messages containing 'hydrat' or 'does not match'.

  3. Compare server HTML and rendered DOM

    Use View Source (the server HTML) and the Elements tab (the live DOM). Differences in dates, prices, IDs or whole blocks point at the cause.

  4. Run the dev buildCustom code

    Run next dev locally. Development mode shows a diff of the expected and actual markup, which names the element.

  5. Test as different visitors

    Try logged-in and logged-out, different time zones and languages, mobile width, and with and without a cookie banner choice. Mismatches often depend on these.

How to fix it

The fix is always the same idea: the first client render must produce the same output as the server. Common causes are below.

Common causes of hydration mismatches and the usual fix
CauseExampleFix
Time or datenew Date().toLocaleString() in renderFormat with a fixed locale and time zone, or set it after mount
Random valuesMath.random() or uuid for idsUse React's useId hook
Browser-only checkstypeof window !== 'undefined' in renderRead browser values inside useEffect
Local storage or cookiesReading localStorage while renderingRead after mount and update state
Invalid HTML nesting<div> inside <p>, <a> inside <a>Correct the markup
Third-party injectionExtensions, translators, widgets editing the DOMTest without them; render the widget client-side only
Render browser-only values after mountjavascript
import { useEffect, useState } from 'react';

export default function LocalTime() {
  const [time, setTime] = useState(null);

  useEffect(() => {
    setTime(new Date().toLocaleTimeString());
  }, []);

  // Server and first client render both output the same placeholder
  return <span>{time ?? ''}</span>;
}
Stable ids with useIdjavascript
import { useId } from 'react';

export function EmailField() {
  const id = useId();
  return (
    <>
      <label htmlFor={id}>Email</label>
      <input id={id} type="email" name="email" />
    </>
  );
}
Where to apply the fix on your stack
  1. Find the component

    Use the dev-mode error diff to locate the element, then search your code for the value that differs.

  2. Make the first render match

    Move browser-only logic into useEffect, use useId for ids, and fix invalid HTML nesting.

  3. Load client-only widgets dynamically

    In Next.js, use next/dynamic with the option { ssr: false } for components that cannot render on the server, such as chat or map widgets.

  4. Last resort for one element

    Add suppressHydrationWarning to a single element whose text is expected to differ, such as a timestamp. It hides the warning for that element only and does not fix other causes.

How to confirm it's fixed

Confirmation checks
  • Reload the page with the console open: no hydration or 'does not match' messages
  • Run a production build (next build, next start) and test again, since some errors differ from dev
  • Submit the form with a test entry and see the success state
  • Confirm the lead reaches your inbox or CRM
  • Check your tag debugger or network tab for the expected tracking hits, once each
  • Repeat on mobile width and in a second browser

How to prevent it happening again

Prevention habits
  • Keep render functions pure: same inputs give the same output
  • Never read window, document, localStorage or the current time during render
  • Use useId for ids and fixed locale and time zone for formatted dates
  • Treat console errors as failures in your review and test process
  • Retest key funnel pages after each deploy and after adding any script or widget
  • Validate HTML nesting with a linter such as eslint-plugin-react rules in CI
Same component, before and after

Mismatch

  • Renders new Date() during render
  • Server and browser output different text
  • Console error on every load
  • Form may reset or lag

Matched

  • Time set in useEffect after mount
  • Server and first client render are identical
  • Clean console
  • Form stays responsive

How FunnelGuard handles this

FunnelGuard detects this today through console errors. A robot visitor runs your real customer journeys on a schedule, from the EU or the US, and records errors the browser logs on each step. A hydration error shows up there as a console error on the page. If it appears, a re-check from a second region runs before one alert goes out, with a screenshot and a likely cause.

FunnelGuard flags the error; it does not rewrite your components, so you still need the fixes above. Because the same journey also submits the form and checks tracking hits and test lead delivery, you can see whether the mismatch actually broke anything. The Free plan covers 1 site and 2 journeys with daily checks and no card, and paid plans include a 3-day free trial.

Questions people ask

What is a hydration mismatch error in React or Next.js?

It is a console error shown when the HTML rendered on the server differs from what React renders first in the browser. React expects them to match so it can attach event handlers to the existing markup. When they differ, it may patch or redraw the affected part of the page.

Why is my Next.js app showing 'Hydration failed because the initial UI does not match'?

A component outputs different content on the server and in the browser. Common causes are dates and times, Math.random or generated ids, window or localStorage reads during render, and invalid HTML nesting like a div inside a p. The development error overlay shows a diff that points at the element.

Does a hydration error break my form or tracking?

Sometimes. Often it only causes flicker, but if React discards the server HTML and re-renders, inputs can reset and handlers can attach late. Tracking that runs on page load may fire late or twice. Submit a test lead and check the tracking hits to see the real effect.

How do I fix a hydration mismatch caused by the date or time?

Do not call new Date() directly during render. Either format with a fixed locale and time zone, or store the value in state and set it inside useEffect so the first render matches the server. For one timestamp element, suppressHydrationWarning is acceptable.

Is suppressHydrationWarning a safe fix?

It is safe only for a single element whose text is expected to differ, like a timestamp. It hides the warning for that element's direct text and attributes but does not fix other mismatches. Using it widely just hides real problems.

Why do I only see the hydration error in production or for some visitors?

Production builds show shorter minified errors, and some mismatches depend on the visitor: time zone, language, login state, cookies or browser extensions. Test a production build, a logged-out session and a clean private window to reproduce it.

Can browser extensions or cookie banners cause hydration errors?

Yes. Extensions, translators and scripts that edit the DOM before React hydrates can change the markup and trigger a mismatch. Test in a clean private window, and load widgets such as consent banners or chat after hydration.

Does FunnelGuard catch this?

Detected

FunnelGuard checks for this today.

How: console error.

Stop finding out from your customers

FunnelGuard runs your real forms and checkouts on a schedule, checks the tracking and the leads behind them, and alerts you once, with a screenshot. Try any paid plan free for 3 days.