我想监控点击是发生在组件内部还是外部。
但我收到错误"未捕获的TypeError:无法读取属性'包含'未定义"
export default class M1Component extends React.Component{
constructor(props) {
super(props);
}
componentWillMount() {
document.addEventListener('click', this.handleClick, false);
}
componentWillUnmount() {
document.removeEventListener('click', this.handleClick, false);
}
handleClick = e => {
if(this.node.contains(e.target)){
console.log("component inside click");
return;
} else {
console.log("component outside click");
}
}
render() {
return(
<div className="form-group" ref={node => this.node = node}>
...
</div>
);
}
}
当我尝试打印这些值时,
console.log(e.target); // getting the div value
console.log(this.refs.node); // getting undefined