0

I have a component that has a child component deep inside it.

In the click event of child component, I have this code:

onClick={(e) => {
    // do something related to the deep child component
    e.stopPropagation()
    e.nativeEvent.stopPropagation()
}}

But this event still bubbles up and the onClick handler of my parent component is still fired.

I read about SyntheticEvent and I also saw these questions. But none of them solved my problem:

ReactJS SyntheticEvent stopPropagation() only works with React events?

How to call stopPropagation in reactjs?

How can I prevent event bubbling in nested React components on click?

Hossein Fallah
  • 1,859
  • 2
  • 18
  • 44
  • Have you tried `e.preventDefault()`? – Serhii Holinei Feb 14 '22 at 12:09
  • @SerhiiHolinei, I added both `e.preventDefault()` and `e.nativeEvent.preventDefault()` but it still fires my parent's `onClick`. Though I think `e.preventDefault()` is all about links and default behavior of elements, not propagation. – Hossein Fallah Feb 14 '22 at 12:12
  • Seems like something else is going on in your setup as calling `stopPropagtion` works. see: https://codesandbox.io/s/competent-hellman-40qoj. Maybe share some more of the code that contains this component and how it's used – thedude Feb 14 '22 at 12:34
  • @thedude it's a real-world application with a lot of complications. I can't share code, because I really don't know what to post here. I just want to know how can I debug it. React's docs are not very informative, or maybe I don't understand them. – Hossein Fallah Feb 14 '22 at 12:43
  • I suggest you try to create a minimal example that reproduces this issue. It will also help you isolate the problem – thedude Feb 14 '22 at 12:45

1 Answers1

0

You can try using onClickCapture() here instead of onClick().

https://reactjs.org/docs/events.html#supported-events

Kislaya Mishra
  • 136
  • 1
  • 11