React event binding details

React event binding details
  • React event binding is similar to native DOM event binding
  • Syntax: on+event name={event handler} Example: onClick={()=>{}}
  • Note: React events use camelCase naming convention

Class component event binding

import React from 'react';
import ReactDOM from 'react-dom';
class App extends React.Component {
  handleClick() {
    console.log(111);
  }
  render() {
    return (<button onClick={this.handleClick} > Click me</button >)
  }
}
ReactDOM.render(<App />, document.getElementById('root'))

Function component event binding

import React from 'react';
import ReactDOM from 'react-dom';
function App() {
  function handleClick() {
    console.log(111);
  }
  // There is no this in the function component, so just write the function name without adding this.
  return (<button onClick={handleClick}>Click me</button>)
}
ReactDOM.render(<App />, document.getElementById('root'))

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

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

<<:  9 Practical CSS Properties Web Front-end Developers Must Know

>>:  MySQL series tutorials for beginners

Recommend

How to install ElasticSearch on Docker in one article

Table of contents Preface 1. Install Docker 2. In...

Linux remote login implementation tutorial analysis

Linux is generally used as a server, and the serv...

JavaScript to achieve stair rolling special effects (jQuery implementation)

I believe everyone has used JD. There is a very c...

Node.js sends emails based on STMP protocol and EWS protocol

Table of contents 1 Node.js method of sending ema...

Vue Element UI custom description list component

This article example shares the specific code of ...

Three networking methods and principles of VMware virtual machines (summary)

1. Brigde——Bridge: VMnet0 is used by default 1. P...

Vue implements multi-grid input box on mobile terminal

Recently, the company has put forward a requireme...

HTML meta viewport attribute description

What is a Viewport Mobile browsers place web page...

Docker build PHP environment tutorial detailed explanation

Docker installation Use the official installation...

Preventing SQL injection in web projects

Table of contents 1. Introduction to SQL Injectio...

Detailed explanation of anonymous slots and named slots in Vue

Table of contents 1. Anonymous slots 2. Named slo...

React Synthetic Events Explained

Table of contents Start by clicking the input box...

The difference and usage of Vue2 and Vue3 brother component communication bus

Table of contents vue2.x vue3.x tiny-emitter plug...