Home > Article > Web Front-end > What are the several ways to write react click events?
Writing method: 1. Use "onClick={this.handleClick}"; 2. Use "onClick={this.handleClick.bind(this)}"; 3. Use "{(params)=> this.handleClick(params)}".
The operating environment of this tutorial: Windows 10 system, react17.0.1 version, Dell G3 computer.
Let’s get straight to the point. Let’s first directly give the correct way to write an event for a button:
Add for a button The correct way to write an onclick event
// handleClick is defined with
onClick = { this.handleClick }
// handleClick用箭头函数定义时,为onClick添加事件应该这么写:<Button onClick = { this.handleClick }></Button>
handleClick is defined like this:
handleClick = () => { // do something here};
or
// handleClick is used (ordinary function) definition
onClick = { this.handleClick .bind(this) }
// handleClick用普通函数定义时,为onClick添加事件应该这么写:<Button onClick = { this.handleClick.bind(this) }></Button>
handleClick is defined as follows:
handleClick () { // do something here }
// handleClick uses < Arrow function>/(ordinary function) can be defined
onClick = { (params) => this.handleClick(params) }
// handleClick可以是箭头函数,也可以是普通函数<Button onClick = { (params) => this.handleClick(params) }></Button>
Let’s analyze why writing like this is correct :
To understand this problem, take the following sentences to understand:
Arrow function, It does not have its own this, so its this is: this
of the context when it is defined. An ordinary function has its own this, so its this is: this
of the context when it is executed. Let’s look at it first. The first way to write:
The code given below is: add an onclick event for a button (a complete jsx)
// examplePage.jsximport React from 'react';import { Button } from 'antd';class examplePage extends React.Component { // 2. 定义handleClick事件 handleClick = () => { console.log(this); //3. this指向examplePage } render() { return ( <p> // 1. 为onClick绑定 handleClick事件处理函数 <Button onClick = { this.handleClick }>click me</Button> </p> ) }}export default examplePage;
Click the button and print out this:
this: examplePage {props: Object, context: {}, refs: {}, updater: Object , state: {}, …}
Analysis:
Let’s look at it first, if bind(this) is not used , what console.log(this) will output:
// examplePage.jsximport React from 'react';import { Button } from 'antd';class examplePage extends React.Component { // 2. !将箭头函数改为普通函数 handleClick () { console.log(this); // 3. this 为 undefined } render() { return ( <p> // 1. 为onClick绑定 handleClick事件处理函数 <Button onClick = { this.handleClick }>click me</Button> </p> ) }}export default examplePage;
Click the button and print out this:
this: undefined
Analysis:
Note: In the strict version, the default this is not Then there is window, but undefined
Module code is always strict mode code.
All parts of a ClassDeclaration or ClassExpression are strict mode code.
So you need to use bind to change this pointer , that is:
render() { return ( <p> // 用bind改变this指向 <Button onClick = { this.handleClick.bind(this) }>click me</Button> </p> )}
Analysis:
After understanding the above two, the last one is easy to understand:
render() { return ( <p> // 通过箭头函数传参 <Button onClick = { (params) => this.handleClick(params) }></Button> </p> )}
Analysis:
Then, when no parameters are passed, just write like this:
<Button onClick = { () => this.handleClick() }></Button>
But this is not feasible Yes, because react will directly parse () => this.handleClick(), handleClick will be called, which is equivalent to onClick = "result of calling handleClick"
So, you can only write like this when no parameters are passed. :
<Button onClick = { this.handleClick }></Button>
Summary
The three questions at the beginning can be answered
//定义handleClick事件 handleClick = () => { // do something here }; //为onClick绑定handleClick事件处理函数 <Button onClick = {this.handleClick}></Button> // 不传参 <Button onClick = {(param) => this.handleClick(param) }></Button> // 传参
推荐学习:《react视频教程》
The above is the detailed content of What are the several ways to write react click events?. For more information, please follow other related articles on the PHP Chinese website!