Full answer
Accessible forms need an id on the input and a label that points to it with htmlFor. If a component hard-codes id="email", then two copies on the same page create duplicate ids, which breaks label clicks and screen reader links. Random ids from Math.random differ between server and client, which causes hydration mismatches.
const id = useId() returns a string that is unique within the app and stable between server and client renders, because it is derived from the component's position in the tree. In React 19 it looks like _r_1_, and the format is not a promise, so treat it as opaque. Never use it as a CSS selector without escaping, since the characters may need escaping.
To make several ids from one call, add a suffix: id + '-error'. This is the pattern for linking an error message with aria-describedby.
useId is for accessibility and DOM linking. It is not for list keys, which should come from your data, and not for database ids. It is also not guaranteed to be unique across separate React roots, so pass identifierPrefix to createRoot if you run several apps on a page.
Design tip: build a small Field component that takes a label and renders the input with an id from useId. Every form then gets correct labelling by default.
The example builds that field, with a hint and an error message linked through aria-describedby.
import { useId } from 'react';
function Field({ label, hint, error }) {
const id = useId();
const hintId = id + '-hint';
const errorId = id + '-error';
return (
<div>
<label htmlFor={id}>{label}</label>
<input
id={id}
aria-invalid={error ? 'true' : 'false'}
aria-describedby={error ? errorId : hintId}
/>
{error ? (
<p id={errorId} role="alert">
{error}
</p>
) : (
<p id={hintId}>{hint}</p>
)}
</div>
);
}
export default function App() {
return (
<form>
<Field label="Email" hint="We never share it" />
<Field label="Backup email" hint="Optional" error="Enter a valid address" />
</form>
);
}Two copies of the same component get different ids, so each label points to its own input. The id format is internal to React and the exact text may change. Rendered result: <form><div><label for="_r_0_">Email</label><input id="_r_0_" aria-invalid="false" aria-describedby="_r_0_-hint"><p id="_r_0_-hint">We never share it</p></div><div><label for="_r_1_">Backup email</label><input id="_r_1_" aria-invalid="true" aria-describedby="_r_1_-error"><p id="_r_1_-error" role="alert">Enter a valid address</p></div></form>
Key points
- useId gives a unique id that is the same on the server and client.
- Use it for htmlFor, aria-describedby and similar linked attributes.
- Build suffixed ids from one call, such as id + '-error'.
- Do not use it for list keys or as a data id.
- Multiple React roots on one page need identifierPrefix.
Common mistakes
- Hard-coding ids in a reusable component.
- Using Math.random or a counter for ids in a server-rendered app.
- Using useId for list keys.
- Using the id in a CSS selector without escaping its characters.
- Calling useId conditionally.
Follow-ups
- Why does a random id cause a hydration mismatch?
- How would you connect an error message to an input for screen readers?
- What is identifierPrefix and when do you need it?
Tips
- Link the answer to accessibility. That is the real purpose.
- Mention hydration safety to show you understand the server side.
- Give the suffix pattern for error and hint text.