[英]Disable eslint error in jsx
I have the below JSX in one of my react-native components我的本机组件之一中有以下 JSX
<TouchableOpacity onPress={this.onEnableNotifications} style={{marginHorizontal: 10}}>
<Image source={require('../../img/ic_warning.png')} style={{tintColor: colorThemes.Blue.red}}/>
</TouchableOpacity>
I get the following ESLint error:我收到以下 ESLint 错误:
'require' is not defined. (no-undef)
I have tried adding line { // eslint-disable-line no-undef }
after Image
but that gives a parsing error.我尝试在
Image
之后添加 line { // eslint-disable-line no-undef }
但这会导致解析错误。 How can I get rid of this error just for that line.我怎样才能摆脱这一行的错误。
On your .eslintrc
file:在您的
.eslintrc
文件中:
{
"globals":{
"require": true
}
}
I've read there are some jsx quirks so try separating it out:我读过有一些 jsx 的怪癖,所以试着把它分开:
<TouchableOpacity onPress={this.onEnableNotifications} style={{marginHorizontal: 10}}>
<Image
source={require('../../img/ic_warning.png')} // eslint-disable-line no-undef
style={{tintColor: colorThemes.Blue.red}}
/>
</TouchableOpacity>
Or you can define it above.或者你可以在上面定义它。
const warningImage = require('../../img/ic_warning.png'); // eslint-disable-line no-undef
....
<TouchableOpacity onPress={this.onEnableNotifications} style={{marginHorizontal: 10}}>
<Image source={warningImage} style={{tintColor: colorThemes.Blue.red}}/>
</TouchableOpacity>
If this is a static path though, I would just define it outside the react class/function entirely, as an import.如果这是一个静态路径,我只会将它完全定义在 react 类/函数之外,作为导入。
In order to disable eslint warnings with comments inside jsx use为了在 jsx 中使用注释禁用 eslint 警告
{ /* eslint-disable no-undef */ }
<div>element causing warning </div>
{ /* eslint-enable no-undef */ }
A single line comment does not work:单行注释不起作用:
{ /* eslint-disable-line no-undef */ }
Also see另见
https://github.com/eslint/eslint/issues/7030 https://github.com/eslint/eslint/issues/7030
To disable an eslint rule on an attribute, you can use an inline comment:要在属性上禁用 eslint 规则,您可以使用内联注释:
<StatusBar
// eslint-disable-next-line react/style-prop-object
style='light'
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.