The implementation of event binding this in React points to three methods

The implementation of event binding this in React points to three methods

1. Arrow Function

1. Take advantage of the fact that the arrow function itself does not bind this;

2. This in the render() method is the component instance, which can get setState();

class App extends React.Component {
    state = {
        count: 0
    }
    // Event handler onIncrement() {
        console.log('this in the event handling function:', this)
        this.setState({
            count:this.state.count+1
        })
    }
    // Rendering render() {
        return (
            <div>
                <h1>{this.state.count}</h1>
              //This in the arrow function points to the external environment, here is: render() method <button onClick={()=>this.onIncrement()}>+1</button>
                {/* <button onClick={this.onIncrement()}>+1</button> */}
            </div>
        )
    }
}

2. Function.proptype.bind()

1. Use the bind method in ES5 to bind this in the event handler to the component instance

class App extends React.Component {
    constructor() {
        super()
        // data this.state = {
            count: 0
        }
        // The first method.bind changes the this pointer, returns a function, and does not execute the function this.onIncrement = this.onIncrement.bind(this)
    }
     // Event handler onIncrement() {
         console.log('this in the event handling function:', this)
         this.setState({
             count:this.state.count+1
         })
     }
    // Rendering render() {
        return (
            <div>
                <h1>{this.state.count}</h1>
                <button onClick={this.onIncrement}>+1</button>
                {/* <button onClick={this.onIncrement()}>+1</button> */}
            </div>
        )
    }
}

3.Class instance methods

1. Using the class instance method in the form of an arrow function

2. This syntax is experimental, but it can be used directly due to the existence of babel

class App extends React.Component {
    constructor() {
        super()
        // data this.state = {
            count: 0
        }
    }
      // Event handler onIncrement=()=> {
        console.log('this in the event handling function:', this)
        this.setState({
            count:this.state.count+1
        })
    }
    // Rendering render() {
        return (
            <div>
                <h1>{this.state.count}</h1>
                <button onClick={this.onIncrement}>+1</button>
                {/* <button onClick={this.onIncrement()}>+1</button> */}
            </div>
        )
    }
}

This concludes this article on the implementation of three methods of event binding this in React. For more relevant React event binding this content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of React event binding
  • Detailed explanation of four ways to bind this to events in React
  • Comparison of several methods of event binding in React learning
  • React event binding details

<<:  Detailed explanation of the idea of ​​installing mysql8.0.11 and changing the root password and connecting navicat for mysql

>>:  Use crontab command in Linux environment to set up scheduled periodic execution tasks [including PHP execution code]

Recommend

Detailed explanation of the function and usage of DOCTYPE declaration

1. Browser rendering mode and doctype Some web pa...

JavaScript to achieve dynamic table effect

This article shares the specific code for JavaScr...

mysql workbench installation and configuration tutorial under centOS

This article shares the MySQL Workbench installat...

How to delete a property of an object in JavaScript

1. delete delete is the only real way to remove a...

Solution to the cross-domain problem of SpringBoot and Vue interaction

Table of contents Browser Same Origin Policy 1. V...

How to modify the firewall on a Linux server to allow remote access to the port

1. Problem Description For security reasons, the ...

How to set up automatic daily database backup in Linux

This article takes Centos7.6 system and Oracle11g...

JavaScript to implement the aircraft war game

This article shares with you how to use canvas an...

Monitor the size change of a DOM element through iframe

A common problem encountered during the developme...

How to use Javascript to generate smooth curves

Table of contents Preface Introduction to Bezier ...

How to install MySQL 8.0 in Docker

Environment: MacOS_Cetalina_10.15.1, Mysql8.0.18,...

Detailed explanation of FTP environment configuration solution (vsftpd)

1. Install vsftpd component Installation command:...

Tutorial on using Multitail command on Linux

MultiTail is a software used to monitor multiple ...