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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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 什么';在ReactJS组件中定义事件处理程序的正确方法是什么?_Javascript_Reactjs_Ecmascript 6 - Fatal编程技术网

Javascript 什么';在ReactJS组件中定义事件处理程序的正确方法是什么?

Javascript 什么';在ReactJS组件中定义事件处理程序的正确方法是什么?,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,使用JSX语法在ReactJS组件中定义事件处理程序有不同的方法 <input onChange={this.handleChange.bind(this)}></input> // inline binding //内联绑定 及 this.handleChange()}>//使用arrow函数 两者都允许handleChange函数访问组件的范围 我一直在使用第一种语法,因为它在可读性方面更清晰 使用一个比另一个有什么优势或用例吗? 如果bind让你烦恼,那么你

使用JSX语法在ReactJS组件中定义事件处理程序有不同的方法

<input onChange={this.handleChange.bind(this)}></input>  // inline binding
//内联绑定

this.handleChange()}>//使用arrow函数
两者都允许
handleChange
函数访问组件的
范围

我一直在使用第一种语法,因为它在可读性方面更清晰

使用一个比另一个有什么优势或用例吗?

如果
bind
让你烦恼,那么你可以这样做

class LoggingButton extends React.Component {
  // This syntax ensures `this` is bound within handleClick.
  // Warning: this is *experimental* syntax.
  handleClick = () => {
    console.log('this is:', this);
  }

  render() {
    return (
      <button onClick={this.handleClick}>
        Click me
      </button>
    );
  }
}

如果
bind
让你烦恼,那么你可以这样做

class LoggingButton extends React.Component {
  // This syntax ensures `this` is bound within handleClick.
  // Warning: this is *experimental* syntax.
  handleClick = () => {
    console.log('this is:', this);
  }

  render() {
    return (
      <button onClick={this.handleClick}>
        Click me
      </button>
    );
  }
}

可以在构造函数中绑定函数:

constructor(){
    super();
    this.handleChange= this.handleChange.bind(this);
}
但最好的方法是使用箭头函数定义方法:

handleChange=()=>{}

在该变体中,每次组件渲染时都将运行绑定:

当需要将某些内容传递给方法时,使用箭头函数:

this.handleChange(params)}>

因为:

将在组件渲染时激发


选中此项。

您可以在构造函数中绑定函数:

constructor(){
    super();
    this.handleChange= this.handleChange.bind(this);
}
但最好的方法是使用箭头函数定义方法:

handleChange=()=>{}

在该变体中,每次组件渲染时都将运行绑定:

当需要将某些内容传递给方法时,使用箭头函数:

this.handleChange(params)}>

因为:

将在组件渲染时激发

看看这个

class Toggle extends React.Component {
  constructor(props) {
    super(props);
    this.state = {isToggleOn: true};
  }

  handleClick(e, something) {
    this.setState(prevState => ({
      isToggleOn: !prevState.isToggleOn
    }));
    alert(something)
  }

  render() {
    return (
      <button onClick={(e) => this.handleClick(e, "Yo!")}>
        {this.state.isToggleOn ? 'ON' : 'OFF'}
      </button>
    );
  }
}

ReactDOM.render(
  <Toggle />,
  document.getElementById('root')
);
constructor(){
    super();
    this.handleChange= this.handleChange.bind(this);
}