Eslint-plugin-jsx-a11y: jsx-a11y/anchor-is-valid

Created on 10 Oct 2017  路  7Comments  路  Source: jsx-eslint/eslint-plugin-jsx-a11y

I have a Link component:

class Link extends React.Component {
  render() {
    const { to, children, ...props } = this.props;
    return (
      <a href={to} {...props} onClick={this.handleClick}>
        {children}
      </a>
    );
  }
}

And use it like this:

<Link to="/somewhere">Link</Link>

But I am getting ESLint error The href attribute is required on an anchor. Provide a valid, navigable address as the href value jsx-a11y/anchor-is-valid

P.S. This Link components is a part of react-starter-kit by kriasoft

Most helpful comment

This worked for me:

.eslintrc

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

Sorry to necro a closed issue, but this came up when searching around to debug a similar issue I had. This was the solution I came up with, figured I'd share it for future debuggers.

All 7 comments

You'll have to provide settings that denote to as a "specialLink" property; see the rule docs.

FYI, here's a link to the rules docs

This worked for me:

.eslintrc

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

Sorry to necro a closed issue, but this came up when searching around to debug a similar issue I had. This was the solution I came up with, figured I'd share it for future debuggers.

It seems like "to" should be specialLink by default here.

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

We needed a click handler that opened a modal, where the a was semantic.

If it鈥檚 an a, it should always have an href as a noJS fallback.

Or put another way - if it doesn鈥檛 have a url, it鈥檚 not semantically a link.

Was this page helpful?
0 / 5 - 0 ratings