Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

e.stopPropagation() seems to not be working as expect. #4335

Closed
luokuning opened this issue Jul 10, 2015 · 9 comments
Closed

e.stopPropagation() seems to not be working as expect. #4335

luokuning opened this issue Jul 10, 2015 · 9 comments

Comments

@luokuning
Copy link

I found this issue when I attempted to integrate ReacJs and a jQuery widget.

I bind an click event on input element and document like follow:

var Search = React.createClass({
    handleClick: function(e) {
        e.stopPropagation();
    },
    render: function() {
        return <input onClick={this.handleClick} />
    }
});
document.addEventListener('click', function() {
    console.log('propagation')
}, false);

And everytime I click the input element, chrome devtool logs 'propagation' message. Should not document element can not receive click event? I'm confused, Did I miss something?

@syranide
Copy link
Contributor

React (currently) uses a listener on the document for (almost) all events, so by the time your component receives the event it has already bubbled all the way up to the document and stopping it only stops it from synthetically bubbling up through the React hierarchy.

Related #2050

@luokuning
Copy link
Author

got it, many thanks :)

@fcsonline
Copy link

You can reproduce this issue in the following jsbin:

http://jsbin.com/rafitiq/edit?html,js,output

fcsonline added a commit to fcsonline/react that referenced this issue Jun 20, 2016
Current React implementation is attaching events to `document` level.
This is breaking bubbling DOM behaviour not letting users to play
properly with methods like `stopPropagtion`.

Also attaching events to containers instead of `document` integrates
better with other Javascript frameworks.

With this change we start attaching events at container level.

Fixes:
facebook#2043
facebook#4335
fcsonline added a commit to fcsonline/react that referenced this issue Jun 20, 2016
Current React implementation is attaching events to `document` level.
This is breaking bubbling DOM behaviour not letting users to play
properly with methods like `stopPropagtion`.

Also attaching events to containers instead of `document` integrates
better with other Javascript frameworks.

With this change we start attaching events at container level.

Fixes:
facebook#2043
facebook#4335
fcsonline added a commit to fcsonline/react that referenced this issue Jun 20, 2016
Current React implementation is attaching events to `document` level.
This is breaking bubbling DOM behaviour not letting users to play
properly with methods like `stopPropagtion`.

Also attaching events to containers instead of `document` integrates
better with other Javascript frameworks.

With this change we start attaching events at container level.

Fixes:
facebook#2043
facebook#4335
fcsonline added a commit to fcsonline/react that referenced this issue Jun 20, 2016
Current React implementation is attaching events to `document` level.
This is breaking bubbling DOM behaviour not letting users to play
properly with methods like `stopPropagation`.

Also attaching events to containers instead of `document` integrates
better with other Javascript frameworks.

With this change we start attaching events at container level.

Fixes:
facebook#2043
facebook#4335
@baurine
Copy link

baurine commented Sep 16, 2018

a workaround, use window.addEventListener() to replace document.addEventListener, event.stopPropagation() can stop event propagate to window.

@JounQin
Copy link

JounQin commented Sep 28, 2018

@baurine You saved my life!

@nettiopsu
Copy link

The change to "window" did not help me, at least when click happened outside of the initial component

@gaearon
Copy link
Collaborator

gaearon commented Aug 10, 2020

We're fixing this in React 17.

@gaearon
Copy link
Collaborator

gaearon commented Aug 11, 2020

If you want to try it:
https://reactjs.org/blog/2020/08/10/react-v17-rc.html

@ghost
Copy link

ghost commented Oct 30, 2020

@gaearon I'm not sure that I got it correctly, but why a case when a user attaches an event listener to a specific DOM node (not to document) isn't working? I've created a fast example here to demonstrate my case.

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

No branches or pull requests

7 participants