美文网首页
react解决 Cannot read property 'st

react解决 Cannot read property 'st

作者: CoderZb | 来源:发表于2020-08-08 10:40 被阅读0次
报错描述

点击按钮,触发chargeFunc函数,很简单的一个操作,却报了如下错误

报错内容如下
image.png
解决办法

chargeFunc(){}改成chargeFunc= (e) => {}


错误写法
class Operation extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      file: "2112",
    };
  }
  render() {
    return (
      <Fragment>
        <Button key="submit" type="primary" onClick={this.chargeFunc}>
          确定{" "}
        </Button>
      </Fragment>
    );
  }
  chargeFunc() {
    console.log("file为", this.state.file);
  }
}

export default Operation;
正确写法
class Operation extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      file: "2112",
    };
  }
  render() {
    return (
      <Fragment>
        <Button key="submit" type="primary" onClick={this.chargeFunc}>
          确定{" "}
        </Button>
      </Fragment>
    );
  }
  chargeFunc= (e) => {
    console.log("file为",this.state.file);
  }

}

export default Operation;

相关文章

网友评论

      本文标题:react解决 Cannot read property 'st

      本文链接:https://www.haomeiwen.com/subject/qmocdktx.html