current position:Home>Event handling of react
Event handling of react
2022-04-29 12:37:58【youhebuke225】
List of articles
Event handling
- stay React The attribute of event binding in needs to be written in the way of hump naming , Such as
onClick
- React The event in is not DOM Native event , But through React Events handled
Event binding
stay react There are three ways to bind events in , The first method is recommended for binding
Example
import React, {
Component, createRef } from "react";
export default class UseRefs extends Component {
constructor(props){
super(props)
this.handleClick3 = this.handleClick3.bind(this)
}
// The first way
handleClick1 = () => {
console.log("hello world");
};
// The third way
handleClick3(){
console.log("hello react hooks");
}
render() {
return (
<>
<button onClick={
this.handleClick1}> Click on 1</button>
{
/* The second way */}
<button onClick={
() => console.log("hello react")}> Click on 2</button>
<button onClick={
this.handleClick3}> Click on 3</button>
</>
);
}
}
The way
stay DOM Direct binding in
- Write it in the way of the above example
- It should be noted that , Be sure to follow an arrow function , hold
this
Point to the outside , If you don't use the arrow function , thatthis
Namelyundefined
Save the method to the property of the class
Like the example above 1 And way 3
The same thing
- The way 1 And way 3 The same thing is that the method of binding events is the same
Difference
- Mainly in functions this The direction of , Because of the way 3 Own your own
this
, So we should bind in the constructorthis
, Let him point to the component instance
Parameter passing
Code
import React, {
Component } from "react";
export default class UseRefs extends Component {
state = {
arr:[2,3,4,6]
}
handleClick = (_index,event) => {
console.log("index",_index);
console.log("event",event);
console.log("event.target",event.target);
};
render() {
return (
<ul>
{
this.state.arr.map((_item,_index) => <li key={
_index} onClick={
(event) => this.handleClick(_index,event)}>{
_item}</li>)}
</ul>
);
}
}
The interpretation of the code
Event object
- To get the event object, we must give a callback function when time binding , The first parameter of the callback function is the event object , Call our defined function in the callback function to pass in the corresponding parameters
- adopt
event.target
Can get the element that triggers the event
Event Bubbling
What is event bubbling Click on
import React, {
Component } from "react";
export default class UseRefs extends Component {
handleClick = () => {
console.log("ul Events above ");
console.log(this);
};
handleClick1 = () => {
console.log(" Click on 1 Events ");
console.log(this);
};
handleClick2 = (event) => {
console.log(" Click on 2 Events ");
console.log(this);
event.stopPropagation();
};
render() {
return (
<ul onClick={
this.handleClick}>
<li onClick={
this.handleClick1}> Click on 1</li>
<li onClick={
event => this.handleClick2(event)}> Click on 2</li>
<li> Click on 3</li>
</ul>
);
}
}
Bubbling
When we click Click on 1 When , We'll find that he triggered two events ( Own events and parent Events )
When we click Click on 3 When ,
You'll find that he also triggered the event , But this incident is really his father's incident
Bubbling mechanism
- Child element has no event binding , Click on the child element , If there is a bound click event on its parent element , Then the click time of the parent element will be triggered
- Child elements have binding events , If the parent element has the same type of binding event , Then the binding event on the parent element will also be triggered
Stop the event from bubbling
stay React in , Use event.stopPropagation()
To stop the bubbling of events , If you click 2
copyright notice
author[youhebuke225],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204291108542929.html
The sidebar is recommended
- JavaScript functions -- pre parsing, Iife, advanced functions, recursion
- JavaScript object
- How to introduce element plus gracefully in vue3 project on demand
- Component slot Vue js
- How to write game interface style in HTML
- Ask some front-end questions.
- Information | driverless taxi has come to Beijing. Xiaoma Zhixing has been approved to carry out automatic driving service in Beijing
- [taro react] - H5 route configuration cancels # on the route
- Reverse advanced, using ast technology to restore JavaScript obfuscated code
- JavaScript object
guess what you like
Vue introduces vant / elementul
JavaScript queue“
Vue content summary
vue3. 0
Small tips: detection of JS font loading failure or completion
CSS is a box that has been climbing
Leetcode215 - the kth largest element in the array - quick sort - quick select - template
CSS adds effect to the four corners of div
HTTP error 500.19 internal server error occurred in the integration pageoffice of aspnetcore project,
Front end < TD > what is the word limit of the label
Random recommended
- Construction and development of automatic test platform based on HTTP runner
- Application of RT thread - use RT thread on stm32l051 (v. conclusion of wireless temperature and humidity sensor)
- Expose Dubbo to HTTP service
- Put the front end into the spring cloud project
- Front end learning day 4 - CSS
- Vue URL jump page with parameters
- JavaScript Chapter 13 traversal of arrays and implicit parameters of function methods
- These CSS efficiency improvement skills, you need to know!
- Beijing has opened the pilot of unmanned operation of passenger cars, and the driverless air outlet has arrived
- Test theory series - Test Case elements and design methods Part II
- How to make the peak value of measured output voltage not less than 2V
- Hexo quickly set up its own blog
- Gee synthetic cloudless landsat-8 and sentry-2 data
- Prototype object creation method case JavaScript specific implementation steps
- YAF CLI and HTTP access modes
- Simple HTML + CSS animation web page production DW simple animation web page production
- The correct way for web programmers to make a pink girl series happy birthday blessing web page (HTML + CSS + JS)
- HTML + CSS + JS to make simple animation web pages
- HTML gourmet web page production DW static web page production div + CSS gourmet web page implementation and production
- Programmer 520 Tanabata Valentine's Day confession code HTML + JS + CSS petal photo album web page template programmer confession necessary
- Element notify notification prompt text wrap
- Belkin: it's too early to talk about real wireless space charging
- JavaScript ES6 set (set) type* ω *
- Understand JavaScript function execution process and scope chain
- Java project: nursing home management system (java + springboot + thymeleaf + HTML + JS + MySQL)
- Java project: drug management system (java + springboot + HTML + layui + bootstrap + seals + MySQL)
- An error is reported when installing hexo during hexo deployment. What is the reason and how to solve it (tag git | keyword path)
- [front end basics] Linux command: Project Automation deployment
- Less than 100 or win Porsche, Maserati "special supply" 3.0T technical analysis
- Introduction to basic methods of math built-in objects in JavaScript
- CSS matches the nth element of the parent element
- What is the commercialization process of unmanned robotaxi after China allows driverless steering wheel?
- A lot of trouble, cook also worried: a quarter less than $8 billion
- Ajax realizes no refresh paging (no refresh paging shopping cart source code)
- [flutter topic] 101 what is flutter elements Yyds dry goods inventory
- What is the commercialization process of unmanned robotaxi after China allows driverless steering wheel?
- A lot of trouble, cook also worried: a quarter less than $8 billion
- Element acquisition of data structure c language sequence stack
- Set video label based on Vue
- CSS realizes div width adaptation, and the proportion of height and width is fixed