道具验证中缺少React eslint错误

cri*_*ego 35 javascript reactjs eslint flowtype

我有下一个代码,eslint throw:

反应/道具类型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;
Run Code Online (Sandbox Code Playgroud)

的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": {}
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

.babelrc

{
  "presets": [
    "es2015",
    "react",
    "stage-0"
  ],
  "whitelist": [
      "es7.decorators",
      "es7.classProperties",
      "es7.exportExtensions",
      "es7.comprehensions",
      "es6.modules"
  ],
  "plugins": ["transform-decorators-legacy"]
}
Run Code Online (Sandbox Code Playgroud)

.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,
  }
}
Run Code Online (Sandbox Code Playgroud)

Omr*_*ron 52

propTypes如果你想在类声明中使用它,你需要定义为静态getter:

static get propTypes() { 
    return { 
        children: PropTypes.any, 
        onClickOut: PropTypes.func 
    }; 
}
Run Code Online (Sandbox Code Playgroud)

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

IxClickOut.propTypes = {
    children: PropTypes.any,
    onClickOut: PropTypes.func,
};
Run Code Online (Sandbox Code Playgroud)

此外,它的更好,如果你输入道具类型从prop-types, react,否则你会看到在控制台警告(如准备阵营16):

import PropTypes from 'prop-types';
Run Code Online (Sandbox Code Playgroud)


Dev*_*ode 40

我知道这个答案很荒谬,但请考虑禁用此规则,直到错误得到解决或您升级了工具:

/* eslint-disable react/prop-types */ // TODO: upgrade to latest eslint tooling
Run Code Online (Sandbox Code Playgroud)

或者在你的 eslintrc 中禁用项目范围:

"rules": {
  "react/prop-types": "off"
}
Run Code Online (Sandbox Code Playgroud)

  • IMO,这是一种不好的做法,不应该推荐给开发人员。禁用规则可能会导致您编写糟糕的代码。(即只要看起来更方便就禁用规则。) (4认同)
  • 禁用此规则的实际语法是: "react/prop-types": "off" (2认同)

air*_*ine 12

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

SomeClass.propTypes = {
    someProp: PropTypes.number,
    onTap: PropTypes.func,
};
Run Code Online (Sandbox Code Playgroud)

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

import PropTypes from 'prop-types';
Run Code Online (Sandbox Code Playgroud)

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

SomeClass.propTypes = {
    someProp: PropTypes.number.isRequired,
    onTap: PropTypes.func.isRequired,
};
Run Code Online (Sandbox Code Playgroud)

或者像这样添加默认的 prop 值:

SomeClass.defaultProps = {
    someProp: 1
};
Run Code Online (Sandbox Code Playgroud)

如果你和我一样,没有经验或不熟悉 reactjs,你也可能会得到这个错误:Must use destructuring props assignment. 要修复此错误,请在使用 props 之前定义它们。例如:

const { someProp } = this.props;
Run Code Online (Sandbox Code Playgroud)


小智 12

安装 prop-types 包 -npm i prop-types --save 导入它 -

import PropTypes from 'prop-types';
Run Code Online (Sandbox Code Playgroud)

然后指定props,我是这样实现的-

Text.propTypes = {
  children: PropTypes.node.isRequired,
};

export default function Text({ children }) {
  return (
    <VStyle className="para">
      <p>{children}</p>
    </VStyle> 
  );
}

Run Code Online (Sandbox Code Playgroud)

还要将其添加到您的 eslintrc.json 或 .js 文件中

"rules": {
    "react/prop-types": "off"
  }
Run Code Online (Sandbox Code Playgroud)


Ali*_*lik 8

似乎问题在于eslint-plugin-react.

propTypes如果您通过在类中的任何位置进行解构来注释命名对象,则无法正确检测提到的道具.

过去也有类似的问题


小智 7

在新版本的 React 和 Next.js 上,您可以简单地导入 PropTypes,如下所示:

import PropTypes from "prop-types";

并在文件底部添加 defaultProps 和 propTypes,例如,

Post.defaultProps = {
  posts: "",
};

Post.propTypes = {
  posts: PropTypes.string,
};

export default Post;
Run Code Online (Sandbox Code Playgroud)

它应该可以解决您的 eslint 警告。


cri*_*ego 2

问题出在handleClick中的流注释中,我删除了它并且工作正常,谢谢@alik