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

Analyze the usage and principles of Vue's provide and inject

First, let's talk about why we use provide/in...

A brief discussion on MySQL B-tree index and index optimization summary

MySQL's MyISAM and InnoDB engines both use B+...

Detailed explanation of MySQL sql_mode query and setting

1. Execute SQL to view select @@session.sql_mode;...

Detailed tutorial on installing nacos in docker and configuring the database

Environment Preparation Docker environment MySQL ...

An example of how Tomcat manages Session

Learned ConcurrentHashMap but don’t know how to a...

Unbind SSH key pairs from one or more Linux instances

DetachKeyPair Unbind SSH key pairs from one or mo...

How to pop up a temporary QQ dialog box to chat online without adding friends

In fact, this is very simple. We add an a tag to ...

Solution to the problem that Docker cannot stop or delete container services

Preface Today, a developer gave me feedback that ...

Three Discussions on Iframe Adaptive Height Code

When building a B/S system interface, you often en...

Solution to Linux server graphics card crash

When the resolution of the login interface is par...

Detailed explanation of MYSQL database table structure optimization method

This article uses an example to illustrate the me...

Detailed explanation of how to use JavaScript paging component

The pagination component is a common component in...

How to write memory-efficient applications with Node.js

Table of contents Preface Problem: Large file cop...

Solution to Nginx SSL certificate configuration error

1. Introduction When a web project is published o...