Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ref未使用react中的箭头函数定义?_Javascript_Reactjs - Fatal编程技术网

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)
}

是否确实共享了正确的沙盒链接可能重复的是否确实共享了正确的沙盒链接可能重复的