useId is a React hook that generates unique IDs that are stable across the server and client. It’s primarily used to create accessible HTML attributes like id
and aria-*
that need to match between server-rendered and client-rendered content.
Syntax: const id = useId();
Key features:
Usage example:
function AccessibleComponent() {
const id = useId();
return (
<div>
<label htmlFor={id}>Name:</label>
<input id={id} type="text" />
</div>
);
}
The useId
hook is one of React’s built-in hooks designed to generate unique IDs that remain consistent between server-side rendering (SSR) and client-side rendering (CSR). It solves common accessibility issues that arise when HTML attributes like id
don’t match between server-rendered and client-rendered content. 🚀
useId
generates a unique string ID that:
import React, { useId } from 'react';
function Component() {
const id = useId();
return (
<div>
<label htmlFor={id}>Input label</label>
<input id={id} type="text" />
</div>
);
}
When the component renders:
function AccessibleForm() {
const nameId = useId();
const emailId = useId();
return (
<form>
<label htmlFor={nameId}>Full name:</label>
<input id={nameId} type="text" name="name" />
<label htmlFor={emailId}>Email:</label>
<input id={emailId} type="email" name="email" />
</form>
);
}
function Accordion() {
const accordionId = useId();
const [isOpen, setIsOpen] = useState(false);
return (
<div>
<button
aria-expanded={isOpen}
aria-controls={`${accordionId}-content`}
onClick={() => setIsOpen(!isOpen)}
>
Toggle accordion
</button>
<div
id={`${accordionId}-content`}
aria-hidden={!isOpen}
>
Accordion content
</div>
</div>
);
}
function Modal({ isOpen, onClose, children }) {
const modalId = useId();
const titleId = useId();
if (!isOpen) return null;
return (
<div
role="dialog"
aria-labelledby={titleId}
aria-describedby={modalId}
>
<h2 id={titleId}>Modal Title</h2>
<div id={modalId}>{children}</div>
<button onClick={onClose}>Close</button>
</div>
);
}
✅ Use useId when:
function AccessibleComponent() {
const id = useId();
return (
<div>
<label htmlFor={id}>Description:</label>
<textarea id={id} name="description" />
</div>
);
}
❌ Avoid useId when:
// ❌ Don't use useId unnecessarily
function SimpleComponent() {
const id = useId(); // Not needed for simple styling
return <div id={id} className="simple-div">Content</div>;
}
// ✅ Just use static IDs
function SimpleComponent() {
return <div id="simple-div" className="simple-div">Content</div>;
}
// ❌ Misuse for general unique IDs
function Component() {
const uniqueId = useId();
return (
<div data-id={uniqueId}>
{/* Using ID for data tracking instead of accessibility */}
</div>
);
}
// ✅ Use for accessibility purposes
function Component() {
const id = useId();
return (
<div>
<label htmlFor={id}>Input:</label>
<input id={id} type="text" />
</div>
);
}
// ❌ Error: conditional hook call
function Component({ showLabel }) {
let id;
if (showLabel) {
id = useId(); // Hook called conditionally!
}
return <div>{showLabel && <label htmlFor={id}>Label</label>}</div>;
}
// ✅ Correctly: always call the hook
function Component({ showLabel }) {
const id = useId(); // Always called
return (
<div>
{showLabel && <label htmlFor={id}>Label</label>}
<input id={id} type="text" />
</div>
);
}
// ❌ Error: same ID for multiple elements
function ListComponent({ items }) {
const id = useId();
return (
<div>
{items.map((item, index) => (
<div key={index}>
<label htmlFor={id}>Item {index}:</label>
<input id={id} type="text" /> {/* Same ID for all inputs! */}
</div>
))}
</div>
);
}
// ✅ Correctly: each item gets its own ID
function ListComponent({ items }) {
return (
<div>
{items.map((item, index) => (
<ItemComponent key={index} item={item} index={index} />
))}
</div>
);
}
function ItemComponent({ item, index }) {
const id = useId();
return (
<div>
<label htmlFor={id}>Item {index}:</label>
<input id={id} type="text" />
</div>
);
}
✅ useId is a React hook for:
✅ When to use:
❌ When to avoid:
✅ Best practices:
useId is a specialized hook for accessibility scenarios, particularly important in server-side rendered applications. Use it when you need to ensure consistent IDs between server and client for proper accessibility. 🚀
Want more articles to prepare for interviews? Subscribe to EasyAdvice, bookmark the site and improve yourself every day 💪