React 学习笔记 - 事件处理

绑定this的常用方法:

1、在构造函数中用bind绑定

this.handleClick = this.handleClick.bind(this);

2、用public class fields语法

 handleClick = () => {
    console.log('this is:', this);
  }

向事件处理程序传递参数:

当我们在向函数传递参数时,如果这样写<button onClick={this.deleteRow(id)}>直接执行函数,不是我们想要的效果,官网给出了两种方式向事件处理函数传递参数

<button onClick={(e) => this.deleteRow(id, e)}>Delete Row</button>
<button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>

在给方法传递新参数时,方法原来的参数会排在新参数之后


猜你喜欢

转载自blog.csdn.net/lala1091/article/details/115549640