Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

reactjs

eslint

In a React app

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

results in 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 point of this error? How do I fix this?

Any help would be greatly appreciated!

like image 660
Eric the Red Avatar asked Dec 18 '17 20:12

Eric the Red


1 Answers

The Link component generates href attribute so in the end anchor tag is valid from the accessibility point of view. Add an exception to .eslintrc:

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

Additionally, there is the same issue with a answer on GitHub.

like image 137
Tomasz Mularczyk Avatar answered Sep 19 '22 12:09

Tomasz Mularczyk