How can I fix jsx-a11y / anchor-is-valid when using Link component in React?

In React App

<Link to={`/person/${person.id}`}>Person Link</Link>

leads to the following eslint error

The href attribute is required on an anchor. Provide a valid, navigable address as the href value jsx-a11y/anchor-is-valid

The Link component results in a valid href attribute.

<a href="#/person/2">Person Link</a>

What is the meaning of this error? How to fix it?

Any help would be greatly appreciated!

+23
source share
4 answers

The Link component generates an href attribute, so at the end the binding tag is valid in terms of accessibility. Add an exception to .eslintrc :

 { "rules": { "jsx-a11y/anchor-is-valid": [ "error", { "components": [ "Link" ], "specialLink": [ "to" ] }] } } 

Also, there is the same issue with the answer on GitHub .

+39
source

this will eliminate the warning

0
source

You can simply write text content inside the "a" tag and hide it with CSS (font-size: 0px or something

-1
source

You might want to put backticks instead of single quotes to create a template literal. Try the following:

 <Link to={`/person/${this.state.id}/edit`}>Edit</Link> 
-5
source

Source: https://habr.com/ru/post/1274170/


All Articles