Category: react
What Is useId?
Published on 27 Oct 2026
Explanation
useId generates an ID suitable for associating accessibility attributes such as labels and inputs. It is designed to remain consistent across server and client rendering when used correctly.
Code:
function EmailField() {
const id = useId();
return (
<>
<label htmlFor={id}>Email</label>
<input id={id} type="email" />
</>
);
}
Explanation
Use a generated ID to connect a label to its input with htmlFor and id. This improves usability for screen-reader users and people who click the label to focus the field.
Code:
function PasswordField() {
const id = useId();
return (
<div>
<label htmlFor={id}>Password</label>
<input id={id} type="password" />
</div>
);
}
Explanation
A generated ID can link a control to help text through aria-describedby, giving assistive technologies additional context about the field.
Code:
function UsernameField() {
const id = useId();
const helpId = `${id}-help`;
return (
<>
<input id={id} aria-describedby={helpId} />
<small id={helpId}>Use at least 4 characters.</small>
</>
);
}
Explanation
You can derive related IDs from one useId result for a component's label, description, and error message. Keep each generated base ID scoped to its component instance.
Code:
function Field() {
const id = useId();
return (
<>
<label htmlFor={`${id}-input`}>Name</label>
<input id={`${id}-input`} aria-describedby={`${id}-description`} />
<small id={`${id}-description`}>Enter your full name.</small>
</>
);
}
Explanation
useId is not intended to generate keys for items in a list. Use stable IDs from your data so React can correctly track items when they are inserted, removed, or reordered.
Code:
function UserList({ users }) {
return users.map((user) => (
<p key={user.id}>{user.name}</p>
));
}