Home  >  Article  >  Web Front-end  >  What is the method to prevent event bubbling in react?

What is the method to prevent event bubbling in react?

WBOY
WBOYOriginal
2022-04-22 10:34:358736browse

Method: 1. Use "e.stopPropagation()" when there is no native event involved in registering only react events; 2. Use "e.nativeEvent.stopImmediatePropagation()" to prevent the outermost document from Events bubble up.

What is the method to prevent event bubbling in react?

The operating environment of this tutorial: Windows 10 system, react17.0.1 version, Dell G3 computer.

What is the method to prevent event bubbling in react

In react, there are two forms of preventing event bubbling:

1, e.stopPropagation() Used when there is no native event registration involved and only react events.

2, e.nativeEvent.stopImmediatePropagation() Use document.addEventListener to register the native event.

React's method of preventing bubbling events is mainly divided into the following two situations:

1. Prevent bubbling between synthetic events, method: e.stopPropagation( );

Event binding is as follows:

The writing method to prevent event bubbling is as follows:


2, Prevent the bubbling between synthetic events and events on the outermost document, method: e.nativeEvent.stopImmediatePropagation ();

Event binding writing method:

(1) No parameters are passed:


(2) Pass parameters:


##Writing to prevent event bubbling:


The reason for writing this is to add a click event to the document, as shown below:

Recommended learning: "react video tutorial"

The above is the detailed content of What is the method to prevent event bubbling in react?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn