简体   繁体   English

如何禁用文件中的 ESLint react/prop-types 规则?

[英]How to disable ESLint react/prop-types rule in a file?

I'm using React and ESLint with eslint-plugin-react .我将ReactESLinteslint-plugin-react一起使用。

I want to disable the prop-types rule in one file.我想在一个文件中disable prop-types规则。

var React = require('react'); 
var Model = require('./ComponentModel');

var Component = React.createClass({
/* eslint-disable react/prop-types */
    propTypes: Model.propTypes,
/* eslint-enable react/prop-types */
    render: function () {
        return (
            <div className="component">
                {this.props.title}
            </div>
        );
    }
});

if you have only one file you want to disable prop-type validation you can use:如果您只有一个文件要禁用 prop-type 验证,则可以使用:

/* eslint react/prop-types: 0 */

in cases where you have multiple files you can add to your .eslintrc file in your root directory a rule to disable prop-type validation:如果您有多个文件,您可以在根目录中的.eslintrc文件中添加禁用 prop-type 验证的规则:

{
 "plugins": [
     "react"
  ],
  "rules": {
    "react/prop-types": 0
  }
}

for further rules you can checkout this link that solved my issue and for inconvenience you can also read up from eslint-plugin-react 's github documentation about how to disable or enable it with various options.有关更多规则,您可以查看解决了我的问题的此链接,如果不方便,您还可以从eslint-plugin-react的 github 文档中阅读有关如何使用各种选项禁用或启用它的文档。

只需将其放在文件的顶部:

/* eslint-disable react/prop-types */

I had to do:我必须做:

/* eslint react/forbid-prop-types: 0 */

this did not work for me:这并没有为我工作:

/* eslint react/prop-types: 0 */

To disable globally in your .eslintrc file (old version v6.0 or below):要在 .eslintrc 文件(旧版本 v6.0 或更低版本)中全局禁用:

{
    "rules": {
        "react/forbid-prop-types": 0
    }
}

To disable globally in your .eslintrc file (new version above v6.0):要在 .eslintrc 文件中全局禁用(v6.0 以上的新版本):

{
    "rules": {
        "react/prop-types": 0
    }
}

I had to wrap the whole component with the eslint ignore comments.我不得不用 eslint 忽略注释来包装整个组件。

var React = require('react'); 
var Model = require('./ComponentModel');

/* eslint-disable react/prop-types */
var Component = React.createClass({

    propTypes: Model.propTypes,

    render: function () {
        return (
            <div className="component">
                {this.props.title}
            </div>
        );
    }
});
/* eslint-enable react/prop-types */

Sometimes I have small components in the same file as the major one.有时我在与主要组件相同的文件中包含小组件。 There propTypes seems overkill.有 propTypes 似乎矫枉过正。 Then I usually do something like this然后我通常会做这样的事情

// eslint-disable-next-line react/prop-types
const RightArrow = ({ onPress, to }) => (<TouchableOpacity onPress={() => onPress(to)} style={styles.rightArrow}><Chevrons.chevronRight size={25} color="grey" /></TouchableOpacity>);

I had to do this in my .eslintrc config file to disable prop types validation error.我必须在我的.eslintrc配置文件中执行此操作以禁用道具类型验证错误。

"rules": {
  "react/prop-types": "off"
}

You can change it to be warning, put it inside eslint rules:您可以将其更改为警告,将其放入eslint规则中:

'react/prop-types': ['warn'],

I needed React relaunch for this to be taken into account without error我需要重新启动 React 才能将其无误地考虑在内

rules: {
  'react/prop-types': 'off'
}

To disable or modify the react/prop-types rule in your .eslint.cjs file, you can use the following configurations:要禁用或修改.eslint.cjs文件中的 react/prop-types 规则,您可以使用以下配置:

Option 1: Disable the rule completely选项 1:完全禁用规则

"rules": {
  "react/prop-types": "off"
}

or或者

"rules": {
  "react/prop-types": 0
}

or或者

"rules": {
  "react/prop-types": false
}

Option 2: Set the rule to generate warnings instead of errors选项 2:设置规则以生成警告而不是错误

"rules": {
  "react/prop-types": ["warn"]
}

Choose the option that best suits your requirements and add it to your .eslint.cjs file.选择最适合您要求的选项并将其添加到您的.eslint.cjs文件中。 This will configure ESLint to either disable the react/prop-types rule or modify its severity to generate warnings instead of errors.这会将 ESLint 配置为禁用 react/prop-types 规则或修改其严重性以生成警告而不是错误。

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

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