Event listener attached to `document` will still be called after calling `event.stopPropagation()`
See original GitHub issueDo you want to request a feature or report a bug?
A bug.
What is the current behavior?
Event listener attached to document
will still be called after calling event.stopPropagation()
on target.
Example: (codesandbox link)
class App extends React.Component {
componentDidMount() {
document.addEventListener("click", this.handleClickOnDocument);
window.addEventListener("click", this.handleClickOnWindow);
}
componentWillUnmount() {
document.removeEventListener("click", this.handleClickOnDocument);
window.removeEventListener("click", this.handleClickOnWindow);
}
handleClickOnDocument = e => {
console.log("handling event on document");
};
handleClickOnWindow = e => {
console.log("handling event on window");
};
render() {
return (
<div onClick={() => console.log("handling event on div")}>
<button
onClick={e => {
e.stopPropagation();
}}
>
Click Me
</button>
</div>
);
}
}
console will log:
handling event on document
What is the expected behavior?
handleClickOnDocument
won’t be called and no log in console.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
- React: both 16.2.0 and 16.3.0
- browser: Chrome 65
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Why event propagates to parent even after calling e ...
Replace document.addEventListener by window.addEventListener so that event.stopPropagation() can stop event propagate to window.
Read more >Event.stopPropagation() - Web APIs | MDN
The stopPropagation() method of the Event interface prevents further propagation of the current event in the capturing and bubbling phases.
Read more >Use `stopImmediatePropogation` to prevent invoking of other ...
I recently added an event listener to a DOM element. ... event listeners the stopPropagation() method will not prevent the other event ......
Read more >Bubbling and capturing - The Modern JavaScript Tutorial
But if event.stopPropagation() is called during the capturing phase, then the event travel stops, no bubbling will occur.
Read more >Event Bubbling and Event Catching in JavaScript and React
React doesn't attach event handlers to nodes – rather to the root of the document instead. When an event is fired, React calls...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
e
inside<div onClick>
handler is a React synthetic event in your example. You’re stopping its propagation through the React hierarchy, but for performance reasons it’s technically implemented as an event listener on the document. So callinge.stopPropagation()
doesn’t affect the browser event and doesn’t prevent it from reaching the document.If you need to forbid it from reaching the document, you could put a ref on the
div
, and useaddEventListener
to add a DOM event listener directly. Thene.stopPropagation()
in it will do what you expect.Hope this helps!
If you want to try it, we released 17 RC yesterday with this change: https://reactjs.org/blog/2020/08/10/react-v17-rc.html