react icon indicating copy to clipboard operation
react copied to clipboard

[react 17] Bug: Click event being picked up by component rendered by the click

Open tholman opened this issue 3 years ago • 4 comments

React version: 17.2

Steps To Reproduce

  1. Using a class component, if a click event changes some state, which renders a new component, and the new component adds an event listener on its componentDidMount checking for clicks, the new component will trigger that event

Link to code example: https://codepen.io/tholman/pen/87c661f0c893a2f8727db927c48d6d12

(this does not happen if the created component uses hooks to add its event listener)

The current behavior

A state change via a click that causes a new component to be rendered, that new component can pick up the original click event that created it, which seems like its reaching back in time, to so speak.

The expected behavior

Perhaps this is now intended behavior with the react 17 event changes (although it doesn't happen if I add the event listener with a hook on a functional component, but I would expect that if a click event causes a render, and the listener is added on the componentDidMount function, I would assume the "click" is complete, and wouldn't be detected.

tholman avatar Jul 06 '21 15:07 tholman

Hey! Can I take up this issue?

akkolahal avatar Aug 26 '21 21:08 akkolahal

This happened to me using React18 and hooks.

Example: https://codesandbox.io/s/festive-mopsa-lgke69?file=/src/App.js

Got a solution using e.stopPropagation() from creating a stackoverflow question. but it's still weird it's happening. https://stackoverflow.com/questions/72315874/react-18-addeventlistentclick-triggers-on-a-click-that-happens-before-compon

BennyCarlsson avatar May 20 '22 09:05 BennyCarlsson

Related or duplicate of https://github.com/facebook/react/issues/24657?

sargalias avatar Oct 23 '22 01:10 sargalias

Duplicate of https://github.com/facebook/react/issues/24657. Check out https://github.com/facebook/react/issues/24657#issuecomment-1150119055 for an extensive explanation

eps1lon avatar Oct 29 '22 09:10 eps1lon