react如何阻止冒泡失败


本文摘自PHP中文网,作者coldplay.xixi,侵删。

react阻止冒泡失败的方法:1、在没有涉及到原生事件注册只有react事件时,用【e.stopPropagation()】阻止冒泡;2、需要用到【e.nativeEvent.stopImmediatePropagation()】方法。

react阻止冒泡失败的方法:

1、在没有涉及到原生事件注册只有react事件时,用e.stopPropagation()阻止冒泡,代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

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、当用document.addEventListener注册了原生的事件后,用e.stopPropagation()是不能阻止与document之间的冒泡,这时候需要用到e.nativeEvent.stopImmediatePropagation()方法,代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

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、阻止合成事件与非合成事件(除了document)之间的冒泡,以上两种方式都不适用,需要用到e.target判断, 代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

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;

相关免费学习推荐:JavaScript(视频)

以上就是react如何阻止冒泡失败的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

React中什么是hoc

一些关于React的常见面试题(分享)

怎么修改React的antd默认样式

React中portal是什么意思

React中怎么安装sass

React组件几种声明方式是什么

React子向父通信有哪些方法?

React中如何优雅的使用svg

React中有实现截图插件吗

React中get与post的区别是什么

更多相关阅读请进入《React》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...