Ant*_*ong 29 eslint react-jsx eslint-config-airbnb
这是我的代码:
const func = () => {
return (
<div >
you're free
</div>
)}
Run Code Online (Sandbox Code Playgroud)
以某种方式,eslint标记"你是免费的"行有错误 error HTML entities must be escaped react/no-unescaped-entities
但是从我可以看到jsx已经逃脱了撇号.我可以看到这些单词you're free没有问题.如果我将其转义为',那么我将很难搜索字符串(我希望you're free在编辑器中搜索返回命中.但显然编辑器会错过,因为文本实际上是you're free)
那么解决这种eslint异常的最佳方法是什么?
Noi*_*art 40
推荐的解决方案是使用',‘或’代替它包装为一个变量.像这样:
const func = () => {
return (
<div >
you're free
</div>
)}
Run Code Online (Sandbox Code Playgroud)
对于搜索功能,建议您拥有本地化/国际化的文件,并将其调用到您的应用程序中.
cri*_*ari 28
我也有同样的问题next.js。我打开.eslintrc.json并添加了以下内容:
{
"rules": { "react/no-unescaped-entities": 0 }
}
Run Code Online (Sandbox Code Playgroud)
现在我的.eslintrc.json意愿如下:
{
"extends": "next/core-web-vitals",
"rules": { "react/no-unescaped-entities": 0 }
}
Run Code Online (Sandbox Code Playgroud)
小智 25
顺便说一句,您可以通过添加来禁用这种警告
"rules": { "react/no-unescaped-entities": 0 }
Run Code Online (Sandbox Code Playgroud)
到您的.eslintrc文件。
Ant*_*ong 12
我通过将行包含在以下内容中显式地转义整个文本块{" "}:
const func = () => {
return (
<div >
{" you're free "}
</div>
)}
Run Code Online (Sandbox Code Playgroud)
你可以做两件事,
它可以通过以下方式逃脱'
例如,
const func = () => {
return (
<div >
you're free
</div>
)}
Run Code Online (Sandbox Code Playgroud)
或者您需要禁用一些 ESLint 规则。要禁用 ESLint 规则,首先.eslintrc.json在项目目录中找到,然后将这些行粘贴到那里。
{
"extends": "next/core-web-vitals",
"rules": {
"react/no-unescaped-entities": "off",
"@next/next/no-page-custom-font": "off"
}
}
Run Code Online (Sandbox Code Playgroud)
上述解决方案仅在某些情况下有效。它对我不起作用。就我而言,我认为这是因为我们也在prettier我们的项目中使用。为了解决这个错误,我用反引号包裹了副本。
const func = () => {
return (
<div>
{`
You're free.
`}
</div>
)
}
Run Code Online (Sandbox Code Playgroud)
小智 6
将其添加到导致错误的文件上
/* eslint-disable react/no-unescaped-entities */
Run Code Online (Sandbox Code Playgroud)
还将其添加到eslintrc.json文件中
{
"extends": "next/core-web-vitals",
"rules": {
"react/no-unescaped-entities": "off",
"@next/next/no-page-custom-font": "off"
}
}
Run Code Online (Sandbox Code Playgroud)