Accessibility Basics for React Web Development
Tags: react, javascript, html, webdev
Published: 2020-04-03 20:21:32


Accessibility is essential to consider when developing web applications. We want our content and services to be available and useful for each and every person. There are various resources and tools at your disposal to ensure you create a React application that can be utilized by as many people as possible. 

**<h1>Semantic HTML</h1>**
Semantic HTML is one of our most important resources. When rendering HTML elements with JSX components, it is best practice to use semantic elements - those with meaning and behavior. The focus is on an HTML element's functionality, rather than appearance. A good example would be to use the `<button>` element when you want to render a button to the DOM, rather than creating a `<span>` with click functionality. 
A semantic element has a clear purpose and it is easy to tell what content it holds.
Semantic code is necessary for screen readers and other assistive technologies to properly interpret what is being rendered to the page. 

<h2>React Fragments</h2>
[Fragments](https://reactjs.org/docs/fragments.html) function much like `<div>` in that you can encapsulate other HTML elements to properly render them to the page with JSX. However, a Fragment is not an HTML element that gets rendered to the DOM like `<div>`. Therefore, Fragment will not break the semantic HTML of your page.

`<Fragment>`s are a good alternative to `<div>` when you need to enclose elements.

 


**<h1>Development Tools</h1>**

<h2>eslint-plugin-jsx-a11y</h2>
Use this with [ESLint](https://www.npmjs.com/package/eslint)- a tool for identifying patterns in ECMAScript and JavaScript code to prevent bugs and support consistent code. 

In your terminal, enter: `npm install eslint --save-dev`

For the react plug-in: `npm install eslint-plugin-react --save-dev`


The [eslint-plugin-jsx-a11y](https://github.com/evcohen/eslint-plugin-jsx-a11y) is an accessibility plug-in for ESLint that provides feedback on how to make your HTML elements more user-friendly. It will provide you with an error, as well as a link to the docs for solutions.

In your terminal, enter: `npm install eslint-plugin-jsx-a11y --save-dev`

Example:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/knfern5cj5ist5a8r2xd.png)

The error shows the word "picture" is redundant in the alt text. Once we remove the word "picture", the error disappears.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5i0mp3fd9u90n32k5jvi.png)


<h2>react-axe</h2>

[react-axe](https://github.com/dequelabs/react-axe) is a library for testing the accessibility support of your web application. It tests the elements being rendered to the DOM during your app's runtime. The results are shown in the Chrome Devtools console. You can download it directly from the chrome web store.
 
**<h1>Conclusion</h1>**

Using accessibility best practices, we can ensure that our technology is inclusive of the diverse groups of people who will be using it. 

This blog is only an intro to accessibility. Below are some useful resources for learning more about it. 

-[React Accessibility Docs](https://reactjs.org/docs/accessibility.html)
-[MDN web docs on HTML elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Element)
-[Fragments](https://dev.to/tumee/react-fragments-what-why-how-2kh1)