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: Manually added event listener always triggered #28742

Open
teamreactwts opened this issue Apr 4, 2024 · 1 comment
Open

Bug: Manually added event listener always triggered #28742

teamreactwts opened this issue Apr 4, 2024 · 1 comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@teamreactwts
Copy link

React version: 18.0.0

Steps To Reproduce

  1. open the sandbox link.
  2. open console.
  3. click on the child text you can see on console there is two log manually added listener and also the react synthetic event.

Link to code example:

https://codesandbox.io/p/sandbox/event-bubble-m7x98y

The current behavior

It always triggers both event if I click on the child text, it triggers manually added parent eventlistener. It should only trigger the onPointerDown event not the parent. I tried to stop the event bubbling using event.stopPropagation() still no success.

Also tried using

new Event("pointerdown",{
bubbles:false,
}) 

but the event is not emitted to child.

The expected behavior

If i click on the child text it should only trigger its onPointerDown event not the parent event.

@teamreactwts teamreactwts added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Apr 4, 2024
@gchust
Copy link

gchust commented Apr 4, 2024

@teamreactwts
This is not a bug, this is because react's PointerDown is different from the browser's native dom event.

Your parent component's event listener was added by the native addEventListner function, but your child component's event is a react's SyntheticEvent which is added by onPointerDown.

It is just like that your parent and child components are in different event cycles.

Changing your parent component to something like the following will fix your issue.

    <div onPointerDown={handlePointerDown2}>
      Parent
      <ChildComponent />
    </div>

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