🪝 Introduction to useId
The useId Hook is a React Hook that generates a unique, stable identifier for a component. These IDs are commonly used to associate related HTML elements, improving accessibility by connecting labels, inputs, descriptions, and other elements.
Important
🎯 Why Use useId?
Many HTML elements require unique IDs so they can reference one another using attributes such as htmlFor, id, and aria-describedby. Manually creating IDs can lead to duplication or inconsistencies. useId automatically generates unique IDs for each component instance.
- Create unique IDs automatically.
- Improve accessibility for forms.
- Avoid duplicate IDs.
- Support both client and server rendering.
⚙️ Syntax
Basic Syntax
const id = useId();| Part | Description |
|---|---|
| useId() | Generates a unique identifier. |
| id | The generated ID string. |
🔄 How useId Works
💻 Example 1: Label and Input
Basic useId Example
import { useId } from "react";
function NameField() {
const id = useId();
return (
<>
<label htmlFor={id}>
Name
</label>
<input
id={id}
type="text"
/>
</>
);
}The generated ID connects the label and the input so that clicking the label focuses the corresponding input, improving accessibility.
💻 Example 2: Multiple Related Elements
Using One ID Prefix
import { useId } from "react";
function PasswordField() {
const id = useId();
return (
<>
<label htmlFor={id}>
Password
</label>
<input
id={id}
aria-describedby={`${id}-hint`}
/>
<p id={`${id}-hint`}>
Must contain at least 8 characters.
</p>
</>
);
}A single generated ID can be extended with suffixes to create related identifiers while keeping them unique.
💻 Example 3: Multiple Component Instances
Unique IDs for Each Component
import { useId } from "react";
function EmailField() {
const id = useId();
return (
<>
<label htmlFor={id}>
Email
</label>
<input id={id} />
</>
);
}Every rendered instance of EmailField automatically receives its own unique ID without additional code.
📊 Manual IDs vs useId
| Manual IDs | useId |
|---|---|
| IDs must be created manually. | IDs are generated automatically. |
| Duplicate IDs are possible. | Each component receives a unique ID. |
| Harder to maintain. | Simplifies component development. |
| May cause accessibility issues. | Supports accessible element relationships. |
📅 ID Generation Timeline
The component renders.
React generates a unique ID.
The ID is assigned to related elements.
Labels, inputs, and descriptions become correctly associated.
The same ID remains stable during subsequent renders.
🎯 Common Use Cases
Connect labels with form controls using htmlFor and id.
Associate elements using accessibility attributes such as aria-labelledby and aria-describedby.
Link helper text, validation messages, or descriptions to input fields.
Build reusable form components without worrying about duplicate IDs.
⚠️ Common Mistakes
- Using useId to generate React list keys.
- Expecting the generated ID to be globally meaningful outside the component.
- Generating separate IDs when a single ID with suffixes would be sufficient.
- Using manually generated random IDs instead of useId for accessibility relationships.
Warning
✅ Best Practices
- Use useId for accessibility-related IDs.
- Reuse one generated ID with suffixes for related elements.
- Use meaningful accessibility attributes such as htmlFor, aria-labelledby, and aria-describedby.
- Continue using stable data IDs for React list keys.
- Keep generated IDs local to the component that owns them.
📚 Official Resource
Learn more about useId in the official React documentation at React useId Documentation.