Fix image-alt in React
In React apps, image-alt failures usually happen when <img src={avatarUrl} /> omits alt or spreads an object (<img {...imgProps} />) whose optional alt field is undefined. Modeling a strict discriminated union (decorative: true vs alt: string) makes missing alt text a TypeScript compile error.
Before and after
// Broken React component: missing alt attribute on informative product photo
export function ProductThumbnail({ src, title }: { src: string; title: string }) {
return (
<figure>
<img src={src} width={320} height={240} className="rounded-md" />
<figcaption>{title}</figcaption>
</figure>
);
}// Fixed React AccessibleImage: requires either descriptive alt or explicit decorative flag
type AccessibleImageProps =
| { src: string; alt: string; decorative?: false; width: number; height: number; className?: string }
| { src: string; decorative: true; alt?: never; width: number; height: number; className?: string };
export function AccessibleImage(props: AccessibleImageProps) {
return (
<img
src={props.src}
alt={props.decorative ? "" : props.alt}
aria-hidden={props.decorative ? true : undefined}
width={props.width}
height={props.height}
className={props.className}
/>
);
}Fixed vs. broken example
Always provide meaningful alt text for informative photos and explicitly set alt="" (empty string) for purely decorative flourishes so screen readers skip them cleanly.
Playwright + axe-core check
import { test, expect } from "@playwright/test";
import AxeBuilder from "@axe-core/playwright";
test("React catalog thumbnails pass axe-core image-alt", async ({ page }) => {
await page.goto("/catalog");
const results = await new AxeBuilder({ page })
.withRules(["image-alt"])
.analyze();
expect(results.violations).toEqual([]);
});Template deduplication tip
One shared <ProductThumbnail /> or <UserAvatar /> missing alt propagates across every list row and detail route. A11yDrift clusters every duplicate page alert back to that single React component.
What NVDA and VoiceOver announce
When an informative image lacks an alt attribute, screen readers either skip it silently or read out raw CDN filenames and hashes, blocking blind shoppers and readers from understanding products, charts, and editorial photos.
NVDA (Windows)
"graphic, cdn_shop_files_IMG_8842_v1731942.webp" (or skipped silently depending on verbosity)
"Cobalt blue stoneware ceramic pour-over mug with matte handle, graphic"
VoiceOver (macOS / iOS)
"IMG_8842_v1731942.webp, image"
"Cobalt blue stoneware ceramic pour-over mug with matte handle, image"
Common questions
Why avoid phrases like 'Image of' or 'Photo of' inside React alt strings?
How should inline SVG charts or icons be labeled in React?
What if a user avatar sits immediately beside the user's visible full name?
Test a URL for image-alt
Enter any public URL for a free WCAG 2.2 AA check—no account or credit card required.
Shipping React weekly? Block new image-alt regressions in PRs with our baseline-diff GitHub Action PR gate (≤20 URLs/PR).