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

Bug: onClick event bubbling to document click which is added after re-render #20140

Closed
vincaslt opened this issue Oct 31, 2020 · 2 comments
Closed
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@vincaslt
Copy link

React version: 17.0.1

Steps To Reproduce

I have a hook, that binds a click event listener on document after some state changes, e.g.:

const [isOpen, setOpen] = useState(false);

useEffect(() => {
  if (!isOpen) {
    return;
  }

  const handleClick = () => {
    console.log('clicked');
  };

  document.addEventListener('click', handleClick);

  return () => {
    document.removeEventListener('click', handleClick);
  };
}, [isOpen]);

return (
  <button
    type="button"
    onClick={() => {
      setOpen(true);
    }}
  >
    Button
  </button>
)

The current behavior

Clicking the button outputs clicked in the console for me.

The expected behavior

This did not happen in the react versions prior to 17, so I assume it has to do something with how events are now bubbling to document events. If I add e.stopPropagation() in my button click handler, the document click event listener is not triggered.

@vincaslt vincaslt added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Oct 31, 2020
@eps1lon
Copy link
Collaborator

eps1lon commented Oct 31, 2020

It doesn't look like this bug report has enough info for one of us to reproduce it. With the code provided I don't see a difference between 16 and 17.

Though it does look similar to #20074. Maybe this is the same issue?

Please provide a CodeSandbox (https://material-ui.com/r/issue-template), a link to a repository on GitHub, or provide a minimal code example that reproduces the problem.

Here are some tips for providing a minimal example: https://stackoverflow.com/help/mcve

@vincaslt
Copy link
Author

Yeah you're right. I use react-transition-group in the component too, which is what the issue is related to it seems, because after removing the usage, everything works as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

2 participants