Skip to content

react 禁止“事件冒泡” #9

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
youngwind opened this issue Dec 18, 2015 · 6 comments
Open

react 禁止“事件冒泡” #9

youngwind opened this issue Dec 18, 2015 · 6 comments
Labels

Comments

@youngwind
Copy link
Owner

标题的说法其实不太准确,因为react原生的事件系统本身就是事件代理的,意味着事件会一直冒泡到document进行绑定。所以,使用普通的**event.stopPropagation();**是没有办法在react中实现阻止事件“冒泡”类似的操作的。
stackoverflow和github issue上都有相关的讨论,比如

  1. http://stackoverflow.com/questions/24415631/reactjs-syntheticevent-stoppropagation-only-works-with-react-events
  2. https://github.com/facebook/react/search?q=stopPropagation&type=Issues&utf8=%E2%9C%93

最后我找到了一个比较简单地解决方法,就是 react-native-listener

// before use react-native-listener
<a style={hotSpotStyle} onClick={this.handleClick}></a>
// after use react-native-listener
<NativeListener key={index} onClick={this.handleClick}>
      <a style={hotSpotStyle}></a>
</NativeListener>

参考地址:https://www.npmjs.com/package/react-native-listener

@Fan-zexu
Copy link

Fan-zexu commented Feb 6, 2017

omg

@Thinking80s
Copy link

@simongfxu 是如何处理的呐?

@SiZapPaaiGwat
Copy link

@Thinking80s 文章已过时,React现在已经没有这个问题了。

https://jsfiddle.net/_alexander_/69z2wepo/34253/

@cqupt-yifanwu
Copy link

大概是什么原理呢?

@Nick930826
Copy link

Nick930826 commented Nov 29, 2017

我用的event.stopPropagation()是可以阻止事件冒泡的

@qq34347476
Copy link

qq34347476 commented Apr 6, 2022

给 Icon 加个样式 pointer-events: none 就能直接解决

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants