Skip to article content
FrontendXLesson 34 / 40

Accessibility in React Components

Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.

intermediate · about 55 minutes · 4 exercises

  • Predict
  • Explain
  • Implement
  • Debug

What you will be able to do

#
  • Prefer semantic elements over ARIA roles.
  • Give controls accessible names and announce status messages.
  • Manage focus for dialogs and dynamic content.

Overview

#

Use the right element

The fastest accessibility win is semantic HTML. A button element is focusable, keyboard-operable and announced as a button. A div with an onClick is none of those. Reach for the native element before adding ARIA.

snippet.tsx
// wrong
<div onClick={save}>Save</div>

// right
<button onClick={save}>Save</button>

Labels and names

Every form control needs an accessible name. Associate a label with htmlFor and id, or wrap the input in the label. An input with only a placeholder has no reliable name.

snippet.tsx
<label htmlFor="email">Email</label>
<input id="email" type="email" />

ARIA only when needed

The first rule of ARIA is not to use it if a native element works. When you must build a custom widget, add the correct role, state and keyboard behavior. A role without the matching keyboard support is worse than no role.

Focus management

Move focus when the context changes: into a dialog on open, back to the trigger on close, and to the first error on a failed submit. Trap focus inside a modal so Tab does not escape it.

Live regions

Status messages that appear without a focus change need a live region so screen readers announce them. Use role status for polite updates and role alert for assertive ones.

snippet.tsx
{error && <p role="alert">{error}</p>}

Pitfall: removing focus outlines

Removing the focus ring without a visible replacement makes the app unusable by keyboard. Style a clear focus indicator instead of removing it.

Motion and testing

Respect prefers-reduced-motion for animations. Run an automated accessibility check such as axe in tests, but treat it as a floor: it catches missing labels and roles, not poor keyboard flow.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    Can a keyboard user activate this control?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.tsx
    <div className="btn" onClick={save}>Save</div>
    Choose the best answer
  2. Exercise 2Implement

    Make this search field accessible with a label and an error live region.

    starter.tsx
    export function Search({ error }: { error?: string }) {
      return <input placeholder="Search" />;
    }

    Reveal the reference answer
    reference.tsx
    export function Search({ error }: { error?: string }) {
      return (
        <div>
          <label htmlFor="q">Search</label>
          <input id="q" placeholder="Search" aria-invalid={error ? true : undefined} aria-describedby={error ? 'q-error' : undefined} />
          {error && <p id="q-error" role="alert">{error}</p>}
        </div>
      );
    }

    Authored expected output: The input has a visible label and its error is announced and associated.

    The label gives the input an accessible name. aria-describedby links the error text to the field, and role alert makes screen readers announce it when it appears.

  3. Exercise 3Debug

    This dialog traps the user. Explain and fix.

    starter.tsx
    function Dialog({ open, children }: { open: boolean; children: React.ReactNode }) {
      return open ? <div role="dialog">{children}</div> : null;
    }

    Reveal the reference answer
    reference.tsx
    function Dialog({ open, onClose, children }: { open: boolean; onClose: () => void; children: React.ReactNode }) {
      const ref = useRef<HTMLDivElement>(null);
      useEffect(() => {
        if (open) ref.current?.focus();
      }, [open]);
      return open ? (
        <div ref={ref} role="dialog" aria-modal="true" tabIndex={-1} onKeyDown={(e) => { if (e.key === 'Escape') onClose(); }}>
          {children}
        </div>
      ) : null;
    }

    Authored expected output: Focus moves into the dialog on open and Escape closes it.

    The dialog needs focus moved into it so keyboard users are inside the widget, and Escape to close. aria-modal tells assistive technology the rest of the page is inert. A full implementation also traps Tab within the dialog.

  4. Exercise 4Explain

    Explain why semantic HTML is preferred over ARIA, and when ARIA is necessary.

    starter.tsx
    // button vs div role=button

    Reveal the reference answer
    reference.tsx
    Semantic HTML gives you accessibility behavior for free: focusability, keyboard activation, roles and states that assistive technology understands. A native button already does all of this. ARIA only describes; it does not add behavior, so a div with role button still needs tabindex and key handlers, and getting them wrong is worse than using the native element. ARIA is necessary when no native element matches the widget, such as a combobox or a tree, and then you must implement the full keyboard interaction and state, not just the role.

    Authored expected output: A written explanation of free behavior from native elements and the conditions for ARIA.

    The core point is that ARIA is descriptive, not functional. Native elements carry behavior; ARIA does not.

Common misconceptions

#
  • A div with onClick is as accessible as a button. It is not focusable or keyboard-operable without extra work.
  • ARIA fixes everything. Native elements are better; ARIA without behavior is worse than nothing.
  • Placeholders are labels. A placeholder is not a reliable accessible name.
  • Automated checks prove accessibility. They catch some issues; keyboard and screen reader testing catch the rest.

Follow-up practice

#
  • react-component-testing
  • react-styling-strategies

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.