Capítulo 55 de 116
useId() generates a stable, unique-per-component-instance ID string safe to use for accessibility attributes (linking a label to an input) — specifically designed to match between server and client rendering, unlike a manually incremented counter.
const id = useId() — no arguments, returns a string unique to that Hook call within the app, stable across re-renders of the same component instance.Math.random() produces different values on the server vs. the client during SSR hydration, causing a mismatch error — useId is specifically built to generate identical IDs on both sides.useId generates one ID per Hook call, not per data item — using it as a substitute for a proper data-derived key (Ch 17) is a misuse; keys must come from your data.useId() call and derive suffixed variants from it (e.g. `${id}-firstName`) rather than calling useId() once per field, keeping them visibly grouped.createRoot/hydrateRoot accept an identifierPrefix option so IDs generated by useId don't collide when multiple independent React roots exist on the same page.function PasswordField() {
const id = useId();
return (
<>
<label htmlFor={id}>Password:</label>
<input id={id} type="password" />
</>
);
}
<label> to its <input> via a generated, SSR-safe id — the canonical use case.useId specifically for accessibility-relevant DOM IDs (label/input associations, aria-describedby targets) — not for list keys, not as a general unique-ID utility.key values, which useId is not.identifierPrefix option lives for multi-root pages.