Javascript ref未使用react中的箭头函数定义?
当用户在元素外部单击时,我尝试创建一个可切换的内容。我得到了此错误。节点未定义错误Javascript ref未使用react中的箭头函数定义?,javascript,reactjs,Javascript,Reactjs,当用户在元素外部单击时,我尝试创建一个可切换的内容。我得到了此错误。节点未定义错误 handleOutsideClick(e) { // ignore clicks on the component itself if (this.node.contains(e.target)) { return; } this.handleClick(); } render() { return ( <div ref={nod
handleOutsideClick(e) {
// ignore clicks on the component itself
if (this.node.contains(e.target)) {
return;
}
this.handleClick();
}
render() {
return (
<div ref={node => { this.node = node; }}>
<button onClick={this.handleClick}>Button handler</button>
{this.state.visibleContent && <div>Toggle content</div>}
</div>
);
}
把手外侧点击(e){
//忽略对组件本身的单击
if(this.node.contains(e.target)){
返回;
}
这个。handleClick();
}
render(){
返回(
{this.node=node;}}>
按钮处理器
{this.state.visibleContent&&Toggle content}
);
}
代码
在渲染方法中,我使用了
ref={node=>{this.node=node;}}
为什么它仍然没有定义?下面是一个使用完全相同技术的工作示例您的函数handleOutside Click
超出范围。如果您使用的是babel,则可以直接将其转换为箭头函数
handleOutsideClick = (e) => {
// ignore clicks on the component itself
if (this.node.contains(e.target)) {
return;
}
this.handleClick();
}
或者,如果这不是一个选项,请在构造函数中绑定它
constructor() {
super()
this.handleClick = this.handleClick.bind(this)
}
您的功能
handleOutside Click
超出范围。如果您使用的是babel,则可以直接将其转换为箭头函数
handleOutsideClick = (e) => {
// ignore clicks on the component itself
if (this.node.contains(e.target)) {
return;
}
this.handleClick();
}
或者,如果这不是一个选项,请在构造函数中绑定它
constructor() {
super()
this.handleClick = this.handleClick.bind(this)
}
是否确实共享了正确的沙盒链接可能重复的是否确实共享了正确的沙盒链接可能重复的