Methods for react to prevent bubbling failure: 1. When there is no native event registration and only react events are involved, use [e.stopPropagation()] to prevent bubbling; 2. You need to use [e.nativeEvent. stopImmediatePropagation()] method.
React's method to prevent bubbling failure:
1. Only react events are registered when there are no native events involved. When using e.stopPropagation()
to stop bubbling, the code is as follows:
import React, { Component } from 'react'; import './App.css'; class App extends Component { handleClickTestBox = (e) => { console.warn('handleClickTestBox: ', e); } handleClickTestBox2 = (e) => { console.warn('handleClickTestBox2: ', e); } handleClickTestBox3 = (e) => { e.stopPropagation(); console.warn('handleClickTestBox3: ', e); } render() { return ( <div className="test-box" onClick={this.handleClickTestBox} > <div onClick={this.handleClickTestBox2} > <div onClick={this.handleClickTestBox3} > </div> </div> </div> ); } } export default App;
2. After using document.addEventListener
to register the native event, use e .stopPropagation() cannot prevent bubbling with the document. In this case, you need to use the e.nativeEvent.stopImmediatePropagation()
method. The code is as follows:
import React, { Component } from 'react'; import './App.css'; class App extends Component { componentDidMount() { document.addEventListener('click', this.handleDocumentClick, false); } handleDocumentClick = (e) => { console.log('handleDocumentClick: ', e); } handleClickTestBox = (e) => { console.warn('handleClickTestBox: ', e); } handleClickTestBox2 = (e) => { console.warn('handleClickTestBox2: ', e); } handleClickTestBox3 = (e) => { // 阻止合成事件的冒泡 e.stopPropagation(); // 阻止与原生事件的冒泡 e.nativeEvent.stopImmediatePropagation(); console.warn('handleClickTestBox3: ', e); } render() { return ( <div className="test-box" onClick={this.handleClickTestBox} > <div onClick={this.handleClickTestBox2} > <div onClick={this.handleClickTestBox3} > </div> </div> </div> ); } } export default App;
3. Prevent synthetic events For bubbling between non-synthetic events (except document), the above two methods are not applicable. e.target
needs to be used to judge. The code is as follows:
import React, { Component } from 'react'; import './App.css'; class App extends Component { componentDidMount() { document.addEventListener('click', this.handleDocumentClick, false); document.body.addEventListener('click', this.handleBodyClick, false); } handleDocumentClick = (e) => { console.log('handleDocumentClick: ', e); } handleBodyClick = (e) => { if (e.target && e.target === document.querySelector('#inner')) { return; } console.log('handleBodyClick: ', e); } handleClickTestBox = (e) => { console.warn('handleClickTestBox: ', e); } handleClickTestBox2 = (e) => { console.warn('handleClickTestBox2: ', e); } handleClickTestBox3 = (e) => { // 阻止合成事件的冒泡 e.stopPropagation(); // 阻止与原生事件的冒泡 e.nativeEvent.stopImmediatePropagation(); console.warn('handleClickTestBox3: ', e); } render() { return ( <div className="test-box" onClick={this.handleClickTestBox} > <div onClick={this.handleClickTestBox2} > <div id="inner" onClick={this.handleClickTestBox3} > </div> </div> </div> ); } } export default App;
related Free learning recommendations: JavaScript (video)
The above is the detailed content of How to prevent bubbling failure in react. For more information, please follow other related articles on the PHP Chinese website!