简体   繁体   English

eslint<a>内部</a>失败<Link><a>在反应</a>

[英]eslint fails on <a> inside <Link> in React

I got Next.js React app.我得到了 Next.js React 应用程序。 Here is my code:这是我的代码:

<Link href="/foo">
  <a>Bar</a>
</Link>

eslint tells me this: The href attribute is required for an anchor to be keyboard accessible. eslint 告诉我这个: The href attribute is required for an anchor to be keyboard accessible. But Next.js wants me to inculde an a tag inside my Link .但是 Next.js 希望我在我的Link中加入一个a标签。 Ho do I configure eslint properly or change my code so that it doesn't warn me about this?我如何正确配置 eslint 或更改我的代码,以便它不会警告我? Aside from completely disabling this rule of course.当然,除了完全禁用此规则。

Add this to .eslintrc.json将此添加到 .eslintrc.json

"jsx-a11y/anchor-is-valid": [ "error", {
        "components": [ "Link" ],
        "specialLink": [ "hrefLeft", "hrefRight" ],
        "aspects": [ "invalidHref", "preferButton" ]
    }]

This setting control the error.此设置控制错误。 I hope this helps我希望这有帮助

eslint-disable-next-line jsx-a11y/anchor-is-valid

如果您使用的是 vscode,只需右键单击问题中的警告,您可以检查是否仅针对该 lino 或整个文件禁用

For NextJS adding "react-app" to extends helped.对于 NextJS,将"react-app"添加到扩展有帮助。

"extends": [
    ...
    "react-app"
],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM