在道具验证中缺少React eslint错误


87

我有下一个代码,随便抛出:

反应/道具类型onClickOut; 道具验证中缺少

反应/道具型儿童;道具验证中缺少

propTypes 已定义,但eslint无法识别。

import React, { Component, PropTypes } from 'react';

class IxClickOut extends Component {
  static propTypes = {
    children: PropTypes.any,
    onClickOut: PropTypes.func,
  };

 componentDidMount() {
    document.getElementById('app')
      .addEventListener('click', this.handleClick);
  }

  componentWillUnmount() {
    document.getElementById('app')
      .removeEventListener('click', this.handleClick);
  }

  handleClick = ({ target }: { target: EventTarget }) => {
    if (!this.containerRef.contains(target)) {
      this.props.onClickOut();
    }
  };

  containerRef: HTMLElement;

  render() {
    const { children, ...rest } = this.props;
    const filteredProps = _.omit(rest, 'onClickOut');

    return (
      <div
        {...filteredProps}
        ref={container => {
          this.containerRef = container;
        }}
      >
        {children}
      </div>
    );
  }
}

export default IxClickOut;

package.json

{
  "name": "verinmueblesmeteor",
  "private": true,
  "scripts": {
    "start": "meteor run",
    "ios": "NODE_ENV=developement meteor run ios"
  },
  "dependencies": {
    "fine-uploader": "^5.10.1",
    "foundation-sites": "^6.2.3",
    "install": "^0.8.1",
    "ix-gm-polygon": "^1.0.11",
    "ix-type-building": "^1.4.4",
    "ix-type-offer": "^1.0.10",
    "ix-utils": "^1.3.7",
    "keymirror": "^0.1.1",
    "meteor-node-stubs": "^0.2.3",
    "moment": "^2.13.0",
    "npm": "^3.10.3",
    "rc-slider": "^3.7.3",
    "react": "^15.1.0",
    "react-addons-pure-render-mixin": "^15.1.0",
    "react-dom": "^15.1.0",
    "react-fileupload": "^2.2.0",
    "react-list": "^0.7.18",
    "react-modal": "^1.4.0",
    "react-redux": "^4.4.5",
    "react-router": "^2.6.0",
    "react-styleable": "^2.2.4",
    "react-textarea-autosize": "^4.0.4",
    "redux": "^3.5.2",
    "redux-form": "^5.3.1",
    "redux-thunk": "^2.1.0",
    "rxjs": "^5.0.0-beta.9",
    "rxjs-es": "^5.0.0-beta.9",
    "socket.io": "^1.4.8"
  },
  "devDependencies": {
    "autoprefixer": "^6.3.6",
    "babel-eslint": "^6.0.4",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-react": "^6.5.0",
    "babel-preset-stage-0": "^6.5.0",
    "core-js": "^2.0.0",
    "cssnano": "^3.7.1",
    "eslint": "^2.12.0",
    "eslint-config-airbnb": "^9.0.1",
    "eslint-import-resolver-meteor": "^0.2.3",
    "eslint-plugin-import": "^1.8.1",
    "eslint-plugin-jsx-a11y": "^1.2.2",
    "eslint-plugin-react": "^5.1.1",
    "node-sass": "^3.8.0",
    "postcss-cssnext": "^2.6.0",
    "sasslets-animate": "0.0.4"
  },
  "cssModules": {
    "ignorePaths": [
      "node_modules"
    ],
    "jsClassNamingConvention": {
      "camelCase": true
    },
    "extensions": [
      "scss",
      "sass"
    ],
    "postcssPlugins": {
      "postcss-modules-values": {},
      "postcss-modules-local-by-default": {},
      "postcss-modules-extract-imports": {},
      "postcss-modules-scope": {},
      "autoprefixer": {}
    }
  }
}

.babelrc

{
  "presets": [
    "es2015",
    "react",
    "stage-0"
  ],
  "whitelist": [
      "es7.decorators",
      "es7.classProperties",
      "es7.exportExtensions",
      "es7.comprehensions",
      "es6.modules"
  ],
  "plugins": ["transform-decorators-legacy"]
}

.eslintrc

{
  "parser": "babel-eslint",
  "extends": "airbnb",
  "rules": {
    "no-underscore-dangle": ["error", { "allow": [_id, b_codes_id] }],
  },
  "settings": {
    "import/resolver": "meteor"
  },
  "globals": {
    "_": true,
    "CSSModule": true,
    "Streamy": true,
    "ReactClass": true,
    "SyntheticKeyboardEvent": true,
  }
}

也许最好写:const { children, onClickOut, ...filteredProps } = this.props;
2016年

您正在使用babel-eslint吗?
蒂莫,2016年

@horyd不是,如果我这样做eslint会抛出no-unused-vars onClickOut已定义但从未使用过
cristian camilocedeñogallego

尝试将其定义为:static get propTypes() { return { children: PropTypes.any, onClickOut: PropTypes.func }; }
Omri Aharon

是的@TimoSta我使用babel-eslint
cristian camilocedeñogallego '16

Answers:


87

如果需要propTypes在类声明中将其定义为静态获取器:

static get propTypes() { 
    return { 
        children: PropTypes.any, 
        onClickOut: PropTypes.func 
    }; 
}

如果要将其定义为对象,则需要在类外部对其进行定义,如下所示:

IxClickOut.propTypes = {
    children: PropTypes.any,
    onClickOut: PropTypes.func,
};

另外,最好从中导入prop类型prop-types而不是 react,否则会在控制台中看到警告(作为React 16的准备):

import PropTypes from 'prop-types';

1
取决于Babel配置,如果您使用静态属性插件,则不一定会更好。
戴夫牛顿

谢谢。第一个选项抛出相同的错误,第二个选项解决了问题,但我不明白为什么在这种情况下定义为class属性会引发错误。注意:我还有另一个可以很好地定义为类属性的组件
cristian camilocedeñogallego,2016年

1
不知道为什么一个失败而另一个失败。我认为无论哪种方式都需要在类中静态定义它,也许我错了。
Omri Aharon'7

14

看来问题出在了eslint-plugin-react

propTypes如果您通过在类中的任何地方进行了结构化注释来命名命名对象,则无法正确检测到提到了哪些道具。

过去也有类似的问题


我正在使用babel-eslint并启用所有阶段注意:我还有另一个可以很好地定义为类属性的组件
cristian camilocedeñogallego,2016年

@cristiancamilocedeñogallego发表相关配置:package.json.eslintrc很难说为什么不选择propTypes
Alik

1
因此问题出在handleClick
Alik

是的@alik删除了流程注释,并且可以正常工作
cristian camilocedeñogallego's

7

在过去的几天里,我遇到了这个问题。就像奥姆里·阿哈隆(Omri Aharon)在上面的回答中所说,为您的道具类型添加类似于以下内容的定义很重要:

SomeClass.propTypes = {
    someProp: PropTypes.number,
    onTap: PropTypes.func,
};

不要忘记在类之外添加prop定义。我会将其放在班级的下方/上方。如果不确定PropType的变量类型或后缀是什么(例如:PropTypes.number),请参考此npm参考。要使用PropType,必须导入包:

import PropTypes from 'prop-types';

如果出现linting错误:someProp is not required, but has no corresponding defaultProps declaration您所要做的就是将其添加.isRequired到prop定义的末尾,如下所示:

SomeClass.propTypes = {
    someProp: PropTypes.number.isRequired,
    onTap: PropTypes.func.isRequired,
};

或添加默认的prop值,如下所示:

SomeClass.defaultProps = {
    someProp: 1
};

如果您像我一样,没有经验或不熟悉reactjs,可能还会收到此错误:Must use destructuring props assignment。要解决此错误,请在使用道具之前先定义它们。例如:

const { someProp } = this.props;

4

我知道这个答案很荒谬,但是请考虑禁用此规则,直到发现错误或升级工具为止:

/* eslint-disable react/prop-types */ // TODO: upgrade to latest craco+eslint or Next.js?

或在eslintrc中禁用整个项目范围:

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

1
禁用此规则的实际语法是:“ react / prop-types”:“ off”
Ken A Collins

谢谢,这也是我在eslintrc中的“规则”部分下使用的内容
iBobb '20


2

问题:道具验证,eslintreact / prop-types中缺少“ id1”

<div id={props.id1} >
    ...
</div>

下面的解决方案在一个功能组件中起作用:

let { id1 } = props;

<div id={id1} >
    ...
</div>

希望能有所帮助。


1

对我来说,将eslint-plugin-react升级到最新版本7.21.5可以解决此问题

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.