A design system component passes its accessibility audit in isolation. Put two <FormField>s on the same settings page — say, "Email" and "Backup email" — and the audit tool flags the second one: label not associated with its control. Click the first label and the first input focuses, exactly like it should. Click the second label and nothing happens.
Open the DOM and the bug is sitting right there: both inputs have id="field-input". Both labels have for="field-input". The browser can only point a for attribute at one element with that id, so it picks the first one it finds — every other instance of the component on the page is invisible to its own label.
The hardcoded id was never going to survive two instances
Here's the component, written the way a first draft usually looks:
function FormField({ label, type = "text" }) { return ( <div className="field"> <label htmlFor="field-input">{label}</label> <input id="field-input" type={type} /> </div> ); } It works the moment you render it once. It breaks the moment you render it twice on the same page, because id has to be unique across the entire document, not just inside one component — and nothing here knows another instance exists.
The next fix people reach for is a counter that increments on every call:
let nextId = 0; function FormField({ label, type = "text" }) { const id = useRef(`field-${nextId++}`).current; return ( <div className="field"> <label htmlFor={id}>{label}</label> <input id={id} type={type} /> </div> ); } This fixes the browser demo. It does not fix the app, for a reason that only shows up once you render on the server: nextId lives at module scope, so it's shared by every request your Node process handles concurrently. Server-render two different users' pages in an overlapping window and their counters interleave — the ids your server sent down for user A's page can come out one request later than user B's, so the numbers each page ends up with don't match what that same component tree would produce on the client. React's hydration step compares server-rendered output to what the client renders on its first pass, and a mismatched id attribute fails that comparison — you get a hydration warning, and in the cases it's actually tied to accessibility wiring (not just the id itself), the labels it renders can briefly point at the wrong field before React patches them up.
Swap the counter for Math.random() or a uuid import and the module-scope race goes away, but you've traded it for a worse version of the same problem: the value is different every time something renders, including the first client render after hydration. Server sends field-0.48213, client generates field-0.91027 for that same component on mount, and now every single page with this component logs a hydration mismatch — not an occasional one under load, every one.
Before you reach for a fourth id-generation scheme: what does React already know about this component that none of these approaches are using?
useId already knows where the component lives
React can see exactly where each component sits in the tree it's rendering — same position on the server as on the client, as long as the tree itself matches. useId reads that position instead of counting calls or rolling dice:
import { useId } from "react"; function FormField({ label, type = "text" }) { const id = useId(); return ( <div className="field"> <label htmlFor={id}>{label}</label> <input id={id} type={type} /> </div> ); } Render two <FormField>s now and each gets its own id — something like «r0» and «r1» — because each one occupies a different position in the tree. Render the same tree on the server and hydrate it on the client, and each instance gets the same id both times, because the id comes from tree position, which hydration guarantees matches, not from a counter or a random seed that has no reason to agree between two separate processes.
The exact shape of that generated id isn't something to build on, and React has changed it before for a very concrete reason: through React 19.1, useId produced ids containing a colon, like :r0: — valid in an HTML id attribute, but not valid in a CSS selector without escaping. Code that ran document.querySelector('#' + id) on one of these broke. React 19.2 reformatted the id specifically to stop that. The lesson survives the format change either way: this value is for linking a label to its control via React's own props, not for you to hang a stylesheet rule or a DOM query off it. If you've ever been tempted to select an element by its generated id from outside React, that's the signal telling you to reach for a ref or a class instead.
Where it stops being the right tool
useId solves exactly one problem — a stable, unique string for accessibility attributes like htmlFor/id, aria-describedby, and aria-labelledby. It is explicitly not for list keys: a key has to track an item of data across reorders, insertions, and deletions, and useId only tracks where a hook call sits in the render tree — reorder the list and the ids reshuffle with the tree, not with the data they're attached to. Keys still come from your data, the same as they always did.
And if your page mounts more than one independent React root — a widget embedded by a third party, a micro-frontend, two separate createRoot calls sharing one HTML document — two roots can each generate their own _r_0_, and now you're back to a collision, just between apps instead of between components. createRoot and hydrateRoot both take an identifierPrefix option for exactly this case, so each root's ids carry a distinguishing prefix instead of starting from the same counter.
Next time an accessibility audit flags a label that works everywhere except the one page where its component shows up twice, check the id before you check the aria attributes. The label was probably never broken — it just had company it didn't know about.
🎮 Try it yourself
▶️ Open the interactive playground →
Runs right in your browser — poke at it and watch the concept react live.
The part worth keeping
A hardcoded id survives exactly one instance. A counter survives one server process. useId is the only one of the three that was actually designed around the fact that your component will render more than once, on more than one machine, and still needs to agree with itself.
How many copies of your own design-system components are already sitting on the same page right now — and which one of their ids would collide if you looked?
🧠 Test yourself
Think it clicked? Take the 8-question quiz →
Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.
📚 Read next
- Your Search Box Lags. Debounce Hides It;
useTransitionFixes It - You're debouncing search input.
useDeferredValueskips the timer. - Your Custom Hooks Tear in React 18.
useSyncExternalStoreFixes It.
🚀 Want more like this? Every guide, playground, and quiz lives on bestpractic.org — open it and sign up free so the next one finds you.
Thanks for reading! Let's stay connected:
- ⭐ GitHub — follow me and star the projects: github.com/parsajiravand
- 💬 Discord — join the frontend best-practices community: discord.gg/d9KRhuAwQ
- 📸 Instagram — frontend best practices, daily: @bestpractice___