Warm tip: This article is reproduced from serverfault.com, please click

React click event not bubbling

发布于 2020-11-30 17:11:50

I having issue in react event bubbling. I have pair with icon inside span element and somewhere upper section element, like this example below:

const App = () => {
  const onClick = (event) => alert(event.target.nodeName);
  return (
    <section onClick={onClick}>
      <span>
        <i className="fas fa-reply"></i> Click me
      </span>
    </section>
  );
};

After click on i tag im waiting for event bubble - to call onClick atleast twice (1st for i, 2nd for span). But it never happens. Could you please tell my why?

I know possible solution - to set pointer-events on i to none, but im more curious why event not bubbling.

Thank you

Questioner
bodynar
Viewed
0
Kushal Jain 2020-12-01 01:21:24

I saw the code you mentioned in the question.

Firstly you didn't added onClick event to

<i/>

tag. First add onClick in it then try to add for span as well. As of now I see you added onClick for section only.