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

reactjs React.js

村村老丝

2020-03-18

我使用ReactESLinteslint-plugin-react

我想disableprop-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>
        );
    }
});

第2035篇《如何禁用文件中的ESLint react / prop-types规则?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

4个回答
Davaid小胖 2020.03.18

有时我在与主要文件相同的文件中包含小的组件。那里的propTypes似乎过于矫kill过正。然后我做这样的事情

// 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>);
StafanDavaid 2020.03.18

如果您只有一个文件要禁用道具类型验证,则可以使用:

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

如果您有多个文件,则可以.eslintrc在根目录中添加一条规则来禁用道具类型验证:

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

要了解更多规则,您可以查看此链接来解决我的问题;不便之处,您还可以阅读eslint-plugin-react的github文档,以了解如何使用各种选项禁用或启用它。

小宇宙GO 2020.03.18

只需将其放在文件顶部即可:

/* eslint react/prop-types: 0 */
宝儿Tom 2020.03.18

我必须做:

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

这并没有为我工作:

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

要在.eslintrc文件中全局禁用:

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

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android