React Prop-Types未检测到ESLint

时间:2017-07-07 08:44:15

标签: javascript reactjs eslint react-proptypes

我的反应页面上有这个:

import PropTypes from 'prop-types';

...然后

  componentWillMount() {
    this.props.myfunction();
  }

......最后:

MyComponent.PropTypes = {
  myfunction: PropTypes.func,
};

为什么ESLine仍抱怨:

" ESLint:' myfunction'道具验证中缺少"

1 个答案:

答案 0 :(得分:2)

尝试使用小写作为propTypes声明:

MyComponent.propTypes = {
  myfunction: PropTypes.func,
};

这里的区别在于PropTypes(captial“P”)是您的节点包,但propTypes(小写)是组件内对象的名称。

演示

class MyApp extends React.Component {
  componentWillMount() {
    this.props.myFunction();
  }
  render() {
    return (<p>hello</p>);
  }
}

MyApp.propTypes = {
  myfunction: PropTypes.func,
};

ReactDOM.render(<MyApp myFunction={() => {console.log("foo bar")}} />, document.getElementById("myApp"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://unpkg.com/prop-types/prop-types.min.js"></script>
<div id="myApp"></div>