useId in ReactThe useId hook in React is used to generate unique IDs for accessibility attributes like id or htmlFor. It’s especially useful when multiple instances of a component exist, and each instance needs its own unique identifier.
useId?useId WorksThe useId hook generates a unique string that you can append to identifiers for better readability.
const id = useId();
useIdHere is an example where using ids will fail. When we click on the second to last label of the element, it will not take us to the correct input!
import React, { useId } from "react";
const ItemForm = () => {
return (
<div>
<label htmlFor="item">Item</label>
<input id="item" type="text" />
</div>
);
};
const App = () => {
return (
<div>
<h1>Multiple Item Forms</h1>
<ItemForm />
<ItemForm />
<ItemForm />
</div>
);
};
export default App;Here’s an example of using useId to fix it and generate unique id attributes for form inputs:
import React, { useId } from "react";
const ItemForm = () => {
const id = useId();
return (
<div>
<label htmlFor={`${id}-item`}>Item</label>
<input id={`${id}-item`} type="text" />
</div>
);
};
const App = () => {
return (
<div>
<h1>Multiple Item Forms</h1>
<ItemForm />
<ItemForm />
<ItemForm />
</div>
);
};
export default App;
ItemForm instance gets a unique id generated by useId.ItemForm might have IDs like r:0-item.ItemForm might have IDs like r:1-item.useIdid attributes for label and input pairs.aria-labelledby or aria-describedby relationships.useId for components rendered multiple times in lists.useId is not reactive — it generates the same ID throughout the component’s lifecycle.useState or a library like uuid.id, htmlFor, ARIA), forms, or dynamic lists.Let me know if you’d like more examples or details! 🚀

Let's start with a problem that useId is trying to solve.

In this form, we conveniently mapped the label to the input through the use of htmlFor in label and id of the input. If you click on the first Item label, it will automatically select the correct input.

But we are rendering multiple instances of this form for many individual items. If you click on the second to third item label, you can notice that the first input is selected.

Moreover, we introduced an HTML inconsistency since multiple inputs now contain the same id: item.

Enter useId.

First, we generate a unique id with the useId hook.

Then, we use the value of id to construct unique IDs of input components. If you click on the individual item label, it will select the correct input. Problem solved!