useId
In one line
useId creates a unique ID for linking label/input, aria attributes — SSR-safe and hydration-safe.
In simple words
useId (React 18) generates a unique ID string that's stable across server and client renders. This prevents hydration mismatches that occur when you generate IDs with Math.random() or Date.now().
Use it for accessibility: linking <label htmlFor> to <input id>, connecting aria-labelledby to elements, etc.
The generated ID looks like :r0:, :r1: — it's not meant for humans. Call useId once per component and append suffixes for multiple elements: id + "-tab", id + "-panel".
Example
import { useId } from "react";
function FormField({ label, ...props }) {
const id = useId();
return (
<div>
<label htmlFor={id}>{label}</label>
<input id={id} {...props} />
</div>
);
}
function Tabs() {
const id = useId();
return (
<>
<button id={`${id}-tab`} aria-controls={`${id}-panel`}>Tab 1</button>
<div id={`${id}-panel`} role="tabpanel">Content</div>
</>
);
}useId generates a stable unique ID for accessibility. Call once and append suffixes for multiple related elements.
Common confusions
Confused with: useRef
The difference: useRef creates a mutable ref object for DOM access — it doesn't generate an ID string. useId generates a string ID for HTML attributes (id, htmlFor, aria-*). They serve different purposes: ref for imperative DOM access, useId for attribute linking.
Related terms
Related Resources
Related Lessons
- React 18 Features
useId.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.