react中console.log(this),this为空?

来源:互联网 发布:二手房增值税算法 编辑:程序博客网 时间:2024/06/18 15:25

因为个人debug的不良方式,喜欢把this打印出来, 发现的问题

如下:很有可能是因为您没有 构造方法

class ImgFigure extends React.Component {/* *imgFigure的點擊處理函數 */ handleClick(e){ console.log(this); this.props.inverse(); e.stopPropagation(); e.preventDefault(); }}

这种情况就写一个constructor把,es6之前叫做getInitial的方法

constructor(props) {    super(props);    this.handleClick = this.handleClick.bind(this);}

0 0