Fix label in React
React forms often fail the label rule when developers render a <span>Email</span> above an <input> instead of a real <label htmlFor>, or hardcode id='email' across reused modals. React's built-in useId() hook generates SSR-safe unique IDs that tie <label htmlFor={id}> directly to <input id={id}>.
Before and after
// Broken React input: uses <div> instead of <label htmlFor>
export function EmailField({ value, onChange }: { value: string; onChange: (v: string) => void }) {
return (
<div className="field">
<div className="field-title">Work email</div>
<input
type="email"
value={value}
placeholder="alex@agency.com"
onChange={(e) => onChange(e.target.value)}
/>
</div>
);
}// Fixed React TextField: SSR-safe useId() binds <label htmlFor> to <input id>
import { useId } from "react";
interface EmailFieldProps {
value: string;
onChange: (value: string) => void;
error?: string;
}
export function EmailField({ value, onChange, error }: EmailFieldProps) {
const inputId = useId();
const errorId = `${inputId}-error`;
return (
<div className="field">
<label htmlFor={inputId} className="field-label">
Work email
</label>
<input
id={inputId}
name="email"
type="email"
autoComplete="email"
value={value}
aria-invalid={error ? true : undefined}
aria-describedby={error ? errorId : undefined}
onChange={(e) => onChange(e.target.value)}
/>
{error && (
<p id={errorId} role="alert">
{error}
</p>
)}
</div>
);
}Fixed vs. broken example
Click directly on the "Work email address" label in the fixed preview: native browsers expand the hit target and immediately move focus into the input.
Playwright + axe-core check
import { test, expect } from "@playwright/test";
import AxeBuilder from "@axe-core/playwright";
test("React signup form inputs are bound to visible labels", async ({ page }) => {
await page.goto("/signup");
await page.getByLabel("Work email").fill("qa@example.com");
const results = await new AxeBuilder({ page })
.withRules(["label"])
.analyze();
expect(results.violations).toEqual([]);
});Template deduplication tip
Fixing your shared <TextField /> component once eliminates unlabeled input failures across login, signup, settings, and checkout routes simultaneously—A11yDrift tracks the regression reduction cleanly across releases.
What NVDA and VoiceOver announce
Placeholder-only inputs lose their hint the moment a user starts typing, cannot be focused by clicking a visible label target, and leave screen-reader and cognitive-disability users guessing which data belongs in each field during checkout or signup.
NVDA (Windows)
"edit, has auto complete, blank" (no field purpose announced)
"Work email address, edit, has auto complete, blank"
VoiceOver (macOS / iOS)
"edit text, Work email address" (inconsistent placeholder fallback that disappears once filled)
"Work email address, edit text"
Common questions
Why use React useId() instead of Math.random() for label IDs?
Does wrapping <input> directly inside <label> also pass axe-core label?
How should validation errors connect to a React input?
Test a URL for label
Enter any public URL for a free WCAG 2.2 AA check—no account or credit card required.
Shipping React weekly? Block new label regressions in PRs with our baseline-diff GitHub Action PR gate (≤20 URLs/PR).