question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Event listener attached to `document` will still be called after calling `event.stopPropagation()`

See original GitHub issue

Do 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:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
gaearoncommented, Apr 2, 2018

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 calling e.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 use addEventListener to add a DOM event listener directly. Then e.stopPropagation() in it will do what you expect.

Hope this helps!

2reactions
gaearoncommented, Aug 11, 2020

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found