eslint:道具验证中没有'isResetProperty'(react / prop-types)

时间:2017-06-13 11:03:12

标签: reactjs eslint

我使用了以下代码,但得到了上面的eslint错误。

componentWillReceiveProps(nextProps) {
        if (nextProps.isResetProperty) {
          this.setState({ empName: defaultEmpName });
          this.setState({ empAddress: defaultEmpAddress });
          this.props.resetPage(false);
        }
      }
  "devDependencies": {
    "eslint": "^3.18.0",
    "eslint-config-airbnb": "^14.1.0",
    "eslint-plugin-import": "^2.2.0",
    "eslint-plugin-jsx-a11y": "^4.0.0",
    "eslint-plugin-react": "^6.10.3",
}

如何解决错误:在道具验证(反应/道具类型)中 [eslint]'isResetProperty'缺失。请帮助我。

2 个答案:

答案 0 :(得分:1)

您需要为isResetProperty属性声明类型。 About PropTypesAbout this rule

完整示例

// index.js
import { Component } from 'react'
import PropTypes from 'prop-types'

export default class Cmp extends Component {
  static propTypes = {
    isResetProperty: PropTypes.bool.isRequired,
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.isResetProperty) {
      this.setState({
        reset: nextProps.isResetProperty,
      })
    }
  }

  render() {
    return null
  }
}

的package.json

  "scripts": {
    "lint": "eslint ."
  },
  "devDependencies": {
    "babel-eslint": "^7.2.3",
    "eslint": "3",
    "eslint-config-airbnb": "^15.0.1",
    "eslint-plugin-import": "^2.3.0",
    "eslint-plugin-jsx-a11y": "^5.0.3",
    "eslint-plugin-react": "^7.0.1"
  },
  "dependencies": {
    "prop-types": "^15.5.10",
    "react": "^15.5.4"
  }

.eslintrc

{
  "extends": "airbnb",
  "parser": "babel-eslint",
  "rules": {
    "semi": [2, "never"]
  }
}

>$ yarn run lint

  

纱线运行v0.24.6   $ eslint。   完成于1.32秒。

答案 1 :(得分:-1)

import React from 'react';
import PropTypes from 'prop-types';

class YourComponentName extends React.Component {

  componentWillReceiveProps(nextProps) {
    if (nextProps.isResetProperty) {
      this.setState({ empName: defaultEmpName });
      this.setState({ empAddress: defaultEmpAddress });
      this.props.resetPage(false);
    }
  }
};

YourComponentName.propTypes = {
  isResetProperty: PropTypes.<type-of-isResetProperty>
}