繁体   English   中英

道具验证[react / prop-types]中缺少调度操作

[英]Dispatch action is missing in props validation [react/prop-types]

我得到像这样的es lint错误[eslint]在道具验证中缺少'getBrodcastedList'[react / prop-types]

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错误定义道具类型

您需要为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. 你应该定义你的函数ES6语法,以便能够访问this.props,this.state里面:
    将“getRequests(query){...}”更改为“getRequests =(query)=> {...}”

  2. 为组件定义PropsType:

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

使用PropTypes进行类型检查

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM