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

    xiaoxiao2021-03-25  58

    因为个人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); }

    转载请注明原文地址: https://ju.6miu.com/read-39133.html

    最新回复(0)