# How to Fix AI Creating React Hydration Mismatches

> AI agents generate React components that render different HTML on the server and client, triggering hydration errors and broken UI on first load.

**Type:** Failure  
**Tools:** Cursor, Claude Code, Codex, Windsurf  
**Stack:** Next.js, Astro, TypeScript  
**Updated:** 2026-06-08

---

The agent writes components that produce different HTML on the server versus the
client — dates, random values, `window` checks — causing React to throw a
hydration mismatch error and re-render the entire tree on load.

## The symptom

A component reads `Date.now()`, `Math.random()`, or `window` during render,
producing different output server-side vs. client-side.

```tsx
// WRONG — hydration mismatch
export function Timestamp() {
  return <p>Page loaded at: {new Date().toLocaleTimeString()}</p>;
  // Server renders "10:00:00 AM", client renders "10:00:01 AM" — mismatch
}

// WRONG — conditional on window
export function ThemeIcon() {
  const isDark = typeof window !== "undefined" && window.matchMedia("(prefers-color-scheme: dark)").matches;
  return <span>{isDark ? "🌙" : "☀️"}</span>;
  // Server: always renders "☀️"; client: may render "🌙" — mismatch
}
```

The browser console shows:

```
Error: Hydration failed because the initial UI does not match what was rendered
on the server.
```

## Why it happens

The agent writes components that seem correct in isolation. It doesn't simulate
two separate render passes (SSR + client hydration) and doesn't know that any
value that differs between the two will break hydration.

## How to spot it

- `Date.now()`, `new Date()`, `Math.random()` called directly in JSX or during
  render without being inside a `useEffect` or `useState` initializer.
- `typeof window !== "undefined"` guards inside the render return — the server
  always takes the `false` branch.
- `localStorage`, `sessionStorage`, or `navigator` read during render.
- Mismatched HTML tags or attribute values between server and client output
  (visible in the React DevTools hydration warning).

## How to fix it

Defer browser-only values to after mount, or generate stable values server-side
and pass them as props.

```tsx
// CORRECT — defer client-only value to after mount
"use client";
import { useState, useEffect } from "react";

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

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

  if (!time) return <p>Loading time…</p>; // matches server output
  return <p>Page loaded at: {time}</p>;
}
```

```tsx
// CORRECT — theme icon: use CSS / data attribute, avoid JS render branch
// Set data-theme on <html> in a blocking inline script (not React)
// Then use CSS: [data-theme="dark"] .icon { content: "🌙" }

// Or use Next.js next-themes which handles SSR safely:
import { useTheme } from "next-themes";
import { useEffect, useState } from "react";

export function ThemeIcon() {
  const { resolvedTheme } = useTheme();
  const [mounted, setMounted] = useState(false);
  useEffect(() => setMounted(true), []);
  if (!mounted) return <span style={{ width: 24 }} />; // stable placeholder
  return <span>{resolvedTheme === "dark" ? "🌙" : "☀️"}</span>;
}
```

```txt
[ ] No Date.now() / Math.random() / new Date() called directly during render
[ ] No typeof window / localStorage / navigator accessed during render
[ ] Browser-only state initializes to null/undefined, set in useEffect
[ ] Render a stable placeholder (same as server output) until after mount
[ ] Use suppressHydrationWarning only on elements where the mismatch is intentional (e.g. timestamp)
[ ] Run "next build && next start" and check browser console for hydration errors
```

## Fix Prompt

```txt title="Fix Prompt"
This component causes a React hydration mismatch because it reads browser-only
values (Date, window, localStorage, Math.random) during render. Fix it: move
all browser-only reads into a useEffect that sets state after mount, render a
stable placeholder on first render that matches what the server produces, and
never conditionally return different JSX trees based on typeof window. Explain
why each change prevents the mismatch.
```

## Test

```bash
# Build and start, then check for hydration errors with curl diff
next build 2>&1 | grep -i "hydrat\|mismatch" && echo "FAIL: hydration error in build" || echo "Build OK"

# Runtime: open browser console after next start and look for hydration warnings
next start &
sleep 3
curl -s http://localhost:3000 | grep -i "data-nextjs-scroll-focus-boundary" > /dev/null && echo "Server rendered OK"
```