Instructor: To begin using React-axe, first we need to install it. We can do so by running npm install, we're going to save this to our development dependencies, and it's called React-axe. Now we can that in our development dependencies here in our package.json file. Now we need to initialize it.
You're going to want to go to whatever file it is that starts up your application. For me and my project here, it's in index.js. We want to get React-axe initialized before we render our first component in our react application.
We want to make sure that React-axe is only running in our development environment, because it's going to be logging errors to the console, and it also has a little bit of a performance hit, so we only want to run it in development, not in our production application.
We can make sure it only runs in our development environment by wrapping it in a block where we check the environment. We're going to check that this is not production. Now inside our block we will dynamically import react-axe, and we will pass to the Axe constructor the React and ReactDOM objects.
Then finally, this third argument is a timing delay in milliseconds for how long react-axe will wait after a component renders, before it will begin analysis again. We're going to go with 1,000. Now that we've got React-axe initialized, we can go ahead and use it.
Here's a sample application I have with some accessibility issues baked in. I've got Chrome dev tools open over here on the right. Note that React-axe works best with Chrome. It's noted to work OK with Firefox and Safari. It's highly recommended that you use it with Chrome at this time.
You can see I have a number of issues being reported in the console now by react-axe. They're ordered by their severity, so we have serious, critical, moderate. A really nice feature of React-axe is that it dedupes findings.
For instance, here, I have two form inputs that have the same issue, and they're only reported here once keeping the console logging nice and clean. When I hover over each of these, it goes ahead and highlights the elements on the page.
If I want to learn more about this finding, they've got links for each of the findings where you can go and learn about the issue. Here they list the severity level which WCAG standard is being violated with this finding, info on how to fix the problem, why it's important, and more information and links to more resources on this issue.
This is a great way to learn more about accessibility and how to solve accessibility problems. If I resolve this issue here, now we can see that that critical finding has been resolved and it's no longer being reported in the console.
Now if we want to configure React-axe to behave differently than the default, we have the ability to pass in a configuration object where we can add new rules, modify existing rules, modify how it reports the console, all sorts of different things we can do to configure React-axe further. We can do this by creating a config object.
If, for instance, we want to modify the rules, we add our rules property which is an array, and it takes an object for each rule we want to either provide or modify. For this example, we're going to modify the radio group rule.
This rule is disabled by default so we're going to enable it. Once we've created our config variable, we can go ahead and pass that as the fourth argument to the Axe constructor. Let's go ahead and change this back to false so we can see the before and after.
Here we have modal in our sample application that has a couple radio buttons. We do have a couple other findings, but one thing we don't have, is whether these radio buttons are in a radio button group. Let's go ahead and turn that rule back on.
Let's change enabled to true, now we can see that finding being reported here in React-axe's logging output. There it is, pointing to our radio buttons. Once again, if we want to know more about this finding and what we can do to fix it, we can just click on the link provided in the console. If we go ahead and fix this issue, we can see that that finding is no longer being reported.