Code Fix Guide

How to Fix React Accessibility Errors

React applications have unique accessibility challenges. JSX makes it easy to create divs with onClick handlers instead of buttons, forget alt text on dynamic images, and build custom components that don't work with keyboards. Here are the most common React accessibility errors and how to fix them.

Common Errors and How to Fix Them

Clickable div instead of button in React
Problem
<div onClick={handleClick}>
  Submit
</div>
Fix
<button onClick={handleClick}>
  Submit
</button>
Dynamic image without alt in JSX
Problem
<img src={product.image} />
Fix
<img src={product.image} alt={product.name} />

{/* For decorative images: */}
<img src={decorative} alt="" role="presentation" />
List items with click handlers but no keyboard support
Problem
{items.map(item => (
  <li onClick={() => select(item)}>
    {item.name}
  </li>
))}
Fix
{items.map(item => (
  <li
    key={item.id}
    role="option"
    tabIndex={0}
    onClick={() => select(item)}
    onKeyDown={(e) => {
      if (e.key === "Enter") select(item);
    }}
    aria-selected={selected === item.id}
  >
    {item.name}
  </li>
))}
Modal without focus trapping or ARIA
Problem
<div className="modal">
  <h2>Confirm</h2>
  <button onClick={close}>Close</button>
</div>
Fix
<div
  className="modal"
  role="dialog"
  aria-modal="true"
  aria-labelledby="modal-title"
>
  <h2 id="modal-title">Confirm</h2>
  <button onClick={close} autoFocus>Close</button>
</div>

Why This Matters

React's component model makes it easy to build inaccessible interfaces without realizing it. Install eslint-plugin-jsx-a11y to catch issues at build time. Use semantic HTML elements (<button>, <a>, <nav>) instead of styled <div>s — they come with keyboard handling and screen reader support built in.

Find All Your Accessibility Issues Automatically

ADA CodeFix scans your website for WCAG 2.1 AA violations and generates AI-powered code fixes — not vague descriptions, actual code you can copy-paste.

Scan Your Site Free
No credit card required AI-generated code fixes