---
title: "How do you fix a hydration error in Next.js?"
description: "A hydration error means the server HTML differs from the first client render. Find the mismatch: dates, random values, window checks or invalid HTML nesting."
url: "https://bugsee.com/answers/nextjs/fix-hydration-error/"
---

# How do you fix a hydration error in Next.js?

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`, `localStorage` or `navigator` during render, so the server and browser see different values.
- Invalid HTML nesting, for example a `div` inside a `p`, which the browser silently rewrites.
- Browser extensions that modify the page before React loads.

```tsx
"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. https://bugsee.com/session-replay/

## Related questions

- [How do you fix "window is not defined" in Next.js?](https://bugsee.com/answers/nextjs/window-is-not-defined/)
