道具验证中缺少调度动作[反应/道具类型]

时间:2019-01-29 04:24:42

标签: reactjs eslint

我收到这样的es lint错误 [eslint]道具验证[react / prop-types]中缺少“ getBrodcastedList”

import { PropTypes } from 'react';
class HelloMessage extends React.Component {


getRequests(query) {
    this.props.getList(query);//this line  [eslint] 'getList' is missing in props validation [react/prop-types]
  }
  render() {
    return (
      <div>
        Hello {this.props.name}
      </div>
    );
  }
}

ReactDOM.render(
  <HelloMessage name="Taylor" />,
  document.getElementById('hello-example')
);

帮帮我。我如何为此定义道具类型 在->“ this.props.getList(query)”上出现es lint错误

2 个答案:

答案 0 :(得分:1)

您需要为getList方法添加propTypes。

import PropTypes from 'prop-types';

class HelloMessage extends React.Component {

getRequests(query) {
    this.props.getList(query);//this line  [eslint] 'getList' is missing in props validation [react/prop-types]
  }
  render() {
    return (
      <div>
        Hello {this.props.name}
      </div>
    );
  }
}

HelloMessage.propTypes = {
  getList: PropTypes.func.isRequired,
};

注意:您需要为其安装另一个软件包prop-types

答案 1 :(得分:1)

  1. 您应该定义函数ES6语法以便能够访问this.props,this.state内部的内容:
    将“ getRequests(query){...}”更改为“ getRequests =(query)=> {...}”

  2. 为您的组件定义PropsType:

HelloMessage.propTypes = {      
    getList: PropTypes.func.isRequired
};

Typechecking With PropTypes