Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.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 反应功能和;这";_Javascript_Reactjs_This - Fatal编程技术网

Javascript 反应功能和;这";

Javascript 反应功能和;这";,javascript,reactjs,this,Javascript,Reactjs,This,我知道“这个”装订和这里的所有其他东西,除了一件事。我不明白为什么“this”在第一次调用中不是未定义的,而是在第二次调用中 另外,我知道函数引用,在第一种情况下它执行函数,但在第二种情况下返回引用 import React from "react"; import ReactDOM from "react-dom"; import "./styles.css"; class App extends React.Component { constructor() { super(

我知道“这个”装订和这里的所有其他东西,除了一件事。我不明白为什么“this”在第一次调用中不是未定义的,而是在第二次调用中

另外,我知道函数引用,在第一种情况下它执行函数,但在第二种情况下返回引用

import React from "react";
import ReactDOM from "react-dom";

import "./styles.css";

class App extends React.Component {
  constructor() {
    super();
    this.name = "MyComponent";
    // this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    console.log(this);
    console.log(this.name);
  }

  render() {
    return (
      <div>
        <button onClick={this.handleClick()}>click 1</button>
        <button onClick={this.handleClick}>click 2</button>
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
从“React”导入React;
从“react dom”导入react dom;
导入“/styles.css”;
类应用程序扩展了React.Component{
构造函数(){
超级();
this.name=“MyComponent”;
//this.handleClick=this.handleClick.bind(this);
}
handleClick(){
console.log(this);
console.log(this.name);
}
render(){
返回(
单击1
点击2
);
}
}
const rootElement=document.getElementById(“根”);
render(,rootElement);
在第一行

<button onClick={this.handleClick()}>click 1</button>
this.handleClick
附加到DOM,当click事件发生时将从DOM的上下文执行,执行将在DOM的上下文中查找handleClick,并且将
未定义

有两种方法可以避免这种情况

  • bind
    像您那样将方法绑定到类

  • 传入一个匿名函数,该函数将在没有DOM上下文的情况下执行,默认情况下绑定到调用方的上下文

  • 像这样

    <button onClick={(e) => this.handleClick(e)}>click 1</button>
    
    this.handleClick(e)}>单击1
    
    在第一行

    <button onClick={this.handleClick()}>click 1</button>
    
    this.handleClick
    附加到DOM,当click事件发生时将从DOM的上下文执行,执行将在DOM的上下文中查找handleClick,并且将
    未定义

    有两种方法可以避免这种情况

  • bind
    像您那样将方法绑定到类

  • 传入一个匿名函数,该函数将在没有DOM上下文的情况下执行,默认情况下绑定到调用方的上下文

  • 像这样

    <button onClick={(e) => this.handleClick(e)}>click 1</button>
    
    this.handleClick(e)}>单击1
    
    通过编写
    onClick={this.handleClick()}
    可以在渲染时直接调用
    this.handleClick
    函数,其计算结果为
    onClick={undefined}
    。这就是为什么单击
    click 1
    按钮没有任何作用。关于为什么
    的行为与
    单击按钮框中的行为相同。请在问题文本中包含相关代码。即使链接将来断开,问题也应该有意义。通过编写
    onClick={this.handleClick()}
    可以直接在渲染时调用
    this.handleClick
    函数,其计算结果为
    onClick={undefined}
    。这就是为什么单击
    click 1
    按钮没有任何作用。关于为什么
    的行为与
    单击按钮框中的行为相同。请在问题文本中包含相关代码。即使将来链接中断,问题也应该有意义。