Accessible React Apps: Beginner's Guide to Accessibility

0 min read 192 words

Accessibility is an important consideration when building modern web apps. React provides useful tools to make accessible, inclusive products.

Let’s look at some best practices for web accessibility with React:

Semantic HTML

Use proper HTML semantics. For example:

// Good
<button>Save</button>

// Avoid 
<div onclick="save">Save</div>

Semantic HTML is parsed correctly by screen readers.

alt Text

Images should have alt text describing content/purpose:

<img src="logo.png" alt="Company logo" />

Screen readers can’t interpret images. alt text substitutes meaning.

ARIA Roles

ARIA roles describe non-semantic elements:

<div role="button">Add</div>

This makes the <div> behave like a button for assistive tech.

Focus Management

Manage focus properly for keyboard/screen reader users:

function Login() {
  const [focused, setFocused] = useState(false);
  
  return (
    <>
      <input 
        onFocus={() => setFocused(true)}
        onBlur={() => setFocused(false)} 
      />
      <button tabIndex={focused ? -1 : 0}>Submit</button>
    </>
  );
}

This shifts focus control to the <input> when focused.

Accessible Forms

Label form fields correctly:

<label htmlFor="name">Name</label>
<input id="name" />

Associates labels with inputs via the htmlFor attribute.

Summary

  • Use proper semantic HTML
  • Provide image alt descriptions
  • Apply ARIA roles appropriately
  • Manage focus and keyboard interaction
  • Label form fields clearly

Building accessible React apps improves experience for all users.

Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags

Recent Posts