useId is a React Hook for generating unique IDs that are stable across the server and client.
Parameters
useId does not take any parameters.
Returns
useId returns a unique ID string associated with this particular useId call in this particular component.
Usage
Generating unique IDs for accessibility attributes
CalluseId at the top level of your component to generate a unique ID:
useId generates a string that includes a colon (:). This ensures the ID is unique but it’s not a valid CSS selector. If you need to use the ID in a CSS selector, add a prefix like id-${useId()}.Generating IDs for several related elements
If you need to give IDs to multiple related elements, you can calluseId once and generate multiple IDs with suffixes:
Specifying a shared prefix for all generated IDs
If you render multiple independent React applications on a single page, pass uniqueidentifierPrefix options to your createRoot or hydrateRoot calls:
Common Use Cases
Form fields with labels
ARIA relationships
- aria-describedby
- aria-labelledby
- aria-controls
Multiple related IDs
Reusable form components
Modal dialogs
TypeScript
Troubleshooting
Why is useId better than an incrementing counter?
You might wonder whyuseId is better than incrementing a global counter:
Can I use useId to generate keys in a list?
No, keys should be generated from your data:The ID contains a colon - can I use it in CSS?
The generated ID contains a colon (:) which is valid in HTML but needs escaping in CSS:
Do I need a different ID for every element?
No! Reuse the same ID for related elements:Best Practices
Use for accessibility, not styling
Extract reusable form components
Combine with other accessibility features
When to use useId
UseuseId when:
- ✅ Connecting form labels to inputs
- ✅ Implementing ARIA relationships (aria-describedby, aria-labelledby, etc.)
- ✅ Creating reusable form components
- ✅ Building accessible custom controls
useId for:
- ❌ List keys (use data IDs or index)
- ❌ Styling selectors (use classes)
- ❌ Database identifiers (use UUIDs or backend-generated IDs)
- ❌ Analytics tracking (use stable identifiers)
Server-Side Rendering
useId is specifically designed to work with SSR. The IDs generated on the server will match the IDs generated during client-side hydration:
useId is better than generating random IDs or using a counter.