useId() (React 18+) returns a unique, stable string id per component instance that is identical on the server and the client, so it's safe for SSR and hydration. Use it to link elements for accessibility: htmlFor/id, aria-describedby, aria-labelledby, aria-controls. Don't use it for list keys or as a database id, and don't use it as a CSS selector without escaping.
let nextId = 0;
function Field() {
const id = `field-${nextId++}`; // breaks with SSR, Strict Mode and concurrent rendering
}
Math.random() is different on every render.useId derives the id from the component's position in the tree (its "parent path"), which is the same on the server and the client.
function PasswordField({ label, hint }) {
const id = useId();
return (
<div>
<label htmlFor={id}>{label}</label>
<input id={id} type="password" aria-describedby={`${id}-hint`} />
<p id={`${id}-hint`}>{hint}</p>
</div>
);
}
Call useId once per component and derive related ids with suffixes. Each <PasswordField /> instance on the page gets its own id, so you can render the component many times without collisions.
:r1:. React 19.1 changed the format to «r1», and React 19.2 changed it again to _r_1_, partly so ids are valid in more CSS selector contexts and for View Transitions. Never parse or depend on the format.querySelector, wrap it with CSS.escape(id). Better still, use a ref.identifierPrefix in createRoot/hydrateRoot (and the matching server render options) so ids from separate apps can't collide.${id}-${item.id}) or move each item into its own component.key={useId()} is wrong. Keys must come from your data and be known when the list is built.useId only matches if the component trees match. Conditional rendering based on typeof window breaks it, along with hydration in general.identifierPrefix for multiple roots.Spotted a mistake or a rights concern? Report an issue