Skip to main content
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

Call useId 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()}.
If you need to give IDs to multiple related elements, you can call useId 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 unique identifierPrefix options to your createRoot or hydrateRoot calls:
This ensures that the IDs generated by the two apps will never clash.

Common Use Cases

Form fields with labels

ARIA relationships

Reusable form components

TypeScript

Troubleshooting

Why is useId better than an incrementing counter?

You might wonder why useId is better than incrementing a global counter:
Problem: This doesn’t work with server-side rendering. During hydration, the component on the server will have a different ID than on the client, causing a mismatch.

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:
Or add a prefix to make it CSS-friendly:

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

Use useId when:
  • ✅ Connecting form labels to inputs
  • ✅ Implementing ARIA relationships (aria-describedby, aria-labelledby, etc.)
  • ✅ Creating reusable form components
  • ✅ Building accessible custom controls
Don’t use 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:
This is why useId is better than generating random IDs or using a counter.