Sub topic 6
Common causes of mismatches
This chapter assumes you know function components, useState and useEffect, and that you have a working server render and a hydrateRoot call. It covers the inputs that make server and client output differ. The fixes are covered in the chapters that follow.
The rule behind every mismatch
During hydration, React renders your components in the browser and compares the result with the HTML the server sent. The first client render must produce the same output as the server did. A mismatch means the two outputs differ. Anything that changes between the two environments can cause one.
Values that differ between server and client
The most common cause is a value that is computed during render and depends on where or when the code runs. Time is the usual example.
function BadClock() {
return <p>{new Date().toLocaleTimeString()}</p>;
}The server renders the time when the request arrived. The browser renders it a moment later, possibly in another time zone and locale. The text differs, so React reports a mismatch.
The same happens with these sources:
| Source | Why it differs |
|---|---|
| `Date.now()`, `new Date()` | Server and browser run at different moments and may use different time zones |
| `Math.random()`, counters | Each environment produces its own values |
| `typeof window !== 'undefined'` | `false` on the server, `true` in the browser |
| `localStorage`, `navigator`, `window.innerWidth` | Not available on the server, or different there |
| Locale formatting | Server and browser may use different default locales |
The browser check deserves a warning. A branch like typeof window === 'undefined' ? <Server /> : <Client /> looks safe, but it makes the first client render differ from the server HTML.
Invalid HTML nesting
The browser parses server HTML before React sees it. If the HTML is invalid, the parser repairs it, and the repaired DOM no longer matches the tree React expects.
function Bad() {
// A div inside a p is invalid HTML, so the browser's parser restructures the DOM
// and the tree React expects no longer matches what it finds.
return (
<p>
<div>Details</div>
</p>
);
}
function Good() {
return (
<div>
<div>Details</div>
</div>
);
}A <div> cannot sit inside a <p>, so the parser closes the paragraph early. Other cases include a <p> inside a <p> and a <table> with <tr> directly inside it, without a <tbody>.
Other sources
- Extensions and injected markup. Browser extensions can change the DOM before hydration runs.
- Different data. If the server and client render from different data, the output differs. This happens when the server uses one fetch result and the client starts with another.
- Whitespace and text edits. Anything that rewrites the HTML in transit, such as a proxy that minifies or injects content, can break the match.
A quick check
When you review a component, look for anything in the render path that reads the clock, a random source, the browser environment, or builds markup that browsers would repair. Each one is a candidate for a mismatch.
Exercise
A component renders {typeof window === 'undefined' ? 'Server' : 'Browser'}. Why does it cause a hydration mismatch?
Show answer
The server renders "Server" and the first client render produces "Browser". The two outputs differ, so React reports a mismatch. The branch depends on the environment instead of on shared data.
Exercise
Why does <p><div>Details</div></p> cause a mismatch even though both server and client run the same component?
Show answer
The browser's HTML parser treats a div inside a p as invalid and closes the paragraph early. The DOM React finds differs from the tree it expects, even though the component code is identical on both sides.