Sub topic 16
A workflow for debugging hydration errors
This chapter assumes you can build a basic SSR setup with renderToPipeableStream and hydrateRoot. It also assumes you know why a mismatch happens: the first client render produces a different tree than the server HTML. Here you will learn a repeatable way to find the component behind an error.
Step 1: Read the error message
React's development build tells you what differed. The message usually names the mismatched text, attribute, or element, and it includes a component stack. Start at the component that is closest to the top of that stack and that you wrote yourself.
In production, error messages are minified. Reproduce the problem in a development build first.
Step 2: Classify the difference
Most mismatches fall into a few groups. Match what you see to a likely cause, then apply the fix from the earlier chapter.
| Symptom | Likely cause | Fix |
|---|---|---|
| Text differs, such as a time or a number | `Date`, `Math.random()`, or locale formatting during render | Move the value into an effect |
| Attribute differs, such as `id` or `for` | A counter or random id | `useId` |
| Server and client disagree on browser state | `window`, `navigator`, `localStorage` read during render | `useSyncExternalStore` with `getServerSnapshot`, or an effect |
| Unexpected element structure | Invalid nesting, such as `div` inside `p` | Correct the markup |
Step 3: Narrow it down
If the stack does not point to one component, bisect the tree.
- Comment out half of the page's children and reload.
- If the error disappears, the cause is in the half you removed. Restore it and split that half again.
- Repeat until one component remains.
Also search that component for the usual suspects: new Date, Math.random, typeof window, navigator, and localStorage.
Step 4: Compare server and client HTML
Use "View Source" in the browser to see the HTML the server sent. Compare it with the Elements panel, which shows the DOM after hydration. Invalid nesting shows up here, because the browser's parser rewrites the HTML before React sees it.
Step 5: Log what React recovered from
Some mismatches are recoverable. React falls back to client rendering for the affected tree, and the page may look fine. Report these so they are not silent.
import { hydrateRoot } from 'react-dom/client';
import App from './App.js';
hydrateRoot(document, <App />, {
onRecoverableError(error, errorInfo) {
console.error('Recoverable error:', error);
console.error('Component stack:', errorInfo.componentStack);
}
});In production, send these to your monitoring service instead of only the console.
Exercise
A page shows the warning "Text content did not match" for a footer. The footer renders © {new Date().getFullYear()}. The server runs in UTC and the error appears only near midnight on New Year's Eve. Which step of the workflow identifies the cause, and what is a fix?
Show answer
Step 2 (classify the difference). The text depends on the current date, which differs between the server render and the client render. Move the year into state set from an effect, so both renders output the same placeholder first. You can also pass the year from the server as a prop so both sides use the same value.