Sub topic 10
When to use suppressHydrationWarning
This chapter assumes you can write function components with useState and useEffect. It also assumes you have seen a hydration mismatch warning, and you know that hydrateRoot attaches React to HTML the server already produced.
What the prop does
Some mismatches are expected. A timestamp rendered on the server will never equal the one the browser computes. suppressHydrationWarning tells React not to warn about a text or attribute difference on one element.
function Timestamp() {
return <time suppressHydrationWarning>{new Date().toISOString()}</time>;
}React still hydrates the element. It does not patch the text, so the browser keeps showing the server's value until something re-renders that element.
The limits
The prop is narrow, and misusing it hides real bugs.
- It applies one level deep. It covers the element it is on, and its direct text and attributes. It does not cover children that are elements.
- It does not repair a mismatch. The server text stays in the DOM. Attributes can also stay out of sync.
- It does not fix structural differences. If the server and client render different elements, the tree can still be wrong.
Choosing between the options
Pick the narrowest tool that matches the cause.
| Situation | Use |
|---|---|
| A value only exists in the browser and users should see it | `useEffect` plus state, as in the earlier clock example |
| An id that must match on both sides | `useId` |
| Browser state such as `navigator.onLine` | `useSyncExternalStore` with `getServerSnapshot` |
| A value that always differs, and showing the server value briefly is acceptable | `suppressHydrationWarning` |
Good fits are timestamps, formatted dates that depend on the user's time zone, and similar leaf text. In these cases the difference is intentional and low-risk.
Combining it with an effect
If the server value is only a placeholder, update it after hydration. Suppression alone leaves the stale text in place.
import { useEffect, useState } from 'react';
function LocalTime({ iso }) {
const [text, setText] = useState(iso);
useEffect(() => {
setText(new Date(iso).toLocaleString());
}, [iso]);
return <time dateTime={iso} suppressHydrationWarning>{text}</time>;
}Here the first client render matches the server, so the prop is not strictly needed. Many teams still prefer the effect alone, because it needs no suppression. Use the prop only when you cannot make the first render identical.
Checklist
- Confirm the mismatch comes from a value that legitimately differs.
- Check that the difference is limited to one element's text or attributes.
- Try an effect,
useId, oruseSyncExternalStorefirst. - If you still suppress, put the prop on the smallest element possible.
Exercise
A page header contains a <p> with the current time and a nested <strong> showing the user's name, which differs between server and client. You add suppressHydrationWarning to the <p>. Does it silence a warning for the name, and what is a better fix for the time?
Show answer
No. The prop works one level deep, so it covers the <p>'s own text and attributes but not the child <strong> element. Fix the name's cause directly. For the time, render a placeholder first and set the real value in useEffect, so the first client render matches the server HTML. Reserve the prop for the leaf <time> element if you must keep it.