---
title: "Hydration failed because the server rendered text didn't match the client"
description: "Why Next.js throws a hydration mismatch when server HTML and the first client render differ, and how to fix dates, random values, browser-only checks and invalid nesting."
url: https://www.danny.engineering/answer/nextjs/hydration-failed-because-the-server-rendered-text-didnt-match-the-client
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:25:00.000Z
tags: ["Next.js"]
---

# Hydration failed because the server rendered text didn't match the client

## The short answer


React found that the HTML the server sent is different from what the same component rendered on the first pass in the browser. Older versions worded this as "Text content does not match server-rendered HTML". The fix is to make the first client render produce exactly the same output as the server, and move anything browser-specific into an effect that runs after hydration.


```typescript
'use client';

import { useEffect, useState } from 'react';

export function LocalTime() {
  const [time, setTime] = useState<string | null>(null);

  useEffect(() => {
    // Runs only in the browser, after hydration
    setTime(new Date().toLocaleTimeString());
  }, []);

  return <span>{time ?? '--:--'}</span>;
}
```


## Why it happens


In the App Router, every component renders on the server first, including Client Components. The browser receives that HTML straight away, then React loads and hydrates it: it renders your components again and attaches event handlers to the existing DOM. React expects both renders to match. When they do not, React reports a hydration error and throws away the server HTML for that part of the tree, rendering it again on the client. You lose some of the benefit of server rendering, and users may see a flash.


The usual causes are:

- Values that change between server and client, such as `new Date()`, `Date.now()` or `Math.random()`.
- Locale and time zone differences. The server might format dates in UTC while the browser uses the user's zone.
- Checks like `typeof window !== 'undefined'` that change what the component returns.
- Reading `localStorage`, `window.innerWidth` or cookies in the browser during render.
- Invalid HTML nesting, such as a `div` inside a `p`, or a `p` inside another `p`. The browser repairs the HTML before React sees it, so the trees no longer match.
- Browser extensions that inject attributes or elements into the page before React loads.

## How to fix it


### Render browser-only values after mounting


The pattern in the short answer is the most reliable. Render a stable placeholder on the server and on the first client render, then fill in the real value in `useEffect`. Effects never run on the server, so both renders match.


### Pass the value from the server


If the server can work out the value, compute it once and pass it down as a prop. Both renders then use the same data.


```typescript
// app/page.tsx (Server Component)
import { Greeting } from './greeting';

export default function Page() {
  const renderedAt = new Date().toISOString();
  return <Greeting renderedAt={renderedAt} />;
}
```


For dates, format with an explicit time zone, for example `Intl.DateTimeFormat('en-GB', { timeZone: 'Europe/London' })`, so the server and browser agree.


### Skip server rendering for one component


For a widget that only makes sense in the browser, such as a chart that measures the window, load it with `next/dynamic` and `ssr: false`. This option must be used inside a Client Component.


```typescript
'use client';

import dynamic from 'next/dynamic';

const Chart = dynamic(() => import('./chart'), { ssr: false });

export function Dashboard() {
  return <Chart />;
}
```


### Fix invalid nesting


Read the error overlay in development. Next.js shows a diff of the server and client trees and often names the element. If it points at nesting, change the outer `p` to a `div`, or move the block element out.


### Suppress a mismatch you cannot avoid


For a single element whose text is meant to differ, such as a timestamp, add `suppressHydrationWarning` to that element. It only works one level deep and should be a last resort, not a blanket fix.


```typescript
<time suppressHydrationWarning>{new Date().toLocaleTimeString()}</time>
```


## Common pitfalls

- Using `typeof window` checks to return different JSX. This is a classic source of the error, because the server and the first client render take different branches.
- Putting `suppressHydrationWarning` on `html` or `body` and assuming it covers the whole page. It does not reach child elements.
- Testing only in a desktop browser with extensions disabled. Ask users which extensions they run if the error only shows in production logs.
- Assuming the error is harmless. React recovers, but it re-renders the tree on the client, which costs performance.

## Related reading

- [Next.js: Text content does not match server-rendered HTML](https://nextjs.org/docs/messages/react-hydration-error)
- [React: hydrateRoot](https://react.dev/reference/react-dom/client/hydrateRoot)
