Sub topic 8
Matching ids on server and client with useId
This chapter assumes you know function components, useState, and useEffect. It also assumes you know what hydration is and why a mismatch between server HTML and the first client render is a problem. Here you will fix one specific source of mismatches: generated ids.
Why generated ids cause mismatches
Form fields need ids so that a <label> can point to its <input> through htmlFor. A common shortcut is to generate the id in the component with a module-level counter or Math.random().
On the server, that code runs once per request. In the browser, it runs again during hydration. Each run produces a different value, so the server HTML contains one id and the first client render expects another. React reports a hydration mismatch.
A counter has a second problem. The server process keeps handling requests, so its counter keeps growing, while each browser starts from zero. The two sides drift apart even if the code looks deterministic.
The fix: useId
useId is a React hook that generates a string id. React derives the string from the component's position in the tree. The server render and the client hydration see the same tree, so they get the same id.
import { useId } from 'react';
function EmailField() {
const id = useId();
return (
<>
<label htmlFor={id}>Email</label>
<input id={id} type="email" />
</>
);
}Call useId once per component and reuse the result. If one component needs several related ids, add a suffix to the single base id:
import { useId } from 'react';
function SignupForm() {
const id = useId();
return (
<form>
<label htmlFor={id + '-name'}>Name</label>
<input id={id + '-name'} />
<label htmlFor={id + '-email'}>Email</label>
<input id={id + '-email'} type="email" />
</form>
);
}Where useId does not belong
- List keys. Keys must come from your data, such as a database id.
useIdidentifies a position in the tree, not an item. - Data identity. Do not store the value in a database or send it to an API. It is only meant to link elements on the page.
Multiple React roots
If you hydrate more than one React root on the same page, their generated ids can collide. Pass the identifierPrefix option to hydrateRoot (and to the matching server render call) so each root uses a different prefix.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Mismatch warning on an `id` or `htmlFor` attribute | Id built from `Math.random()`, `Date.now()`, or a counter | Replace it with `useId` |
| Clicking a label does not focus its input | Two elements share one id | Use a suffix per element |
| Ids collide across separate roots | No `identifierPrefix` | Set a distinct prefix per root |
Exercise
A component calls Math.random() to build an id for a text input, and hydration logs a mismatch on the id attribute. What change fixes it, and why does it work?
Show answer
Replace the random value with const id = useId(); and use it for both id and htmlFor. React derives the id from the component's position in the tree, so the server render and the client hydration produce the same string.
Exercise
A form component needs ids for three inputs. How many times should it call useId?
Show answer
Once. Call it a single time and append a suffix to the result for each input, for example id + '-name'.