Description
This is a weird one. Basically, if you add an event listener to the document in an effect that was triggered by an event. e.g. click
toggles some state, which triggers an effect, which adds a click
handler to the document. In the normal case the new event handler will "miss" the triggering event, e.g. the added click handler won't respond to the click event that triggered it being added (omg).
HOWEVER, if you render a portal first, the timing changes slightly and the added event handler will see the current event.
React version: 17
Steps To Reproduce
https://codesandbox.io/s/react-playground-forked-cyt0f?file=/index.js
- Click the "show Message" button to see a message toggle in and out
- Click the "Render Portal" button (see a portal rendered into the body)
- Click the "show Message" button again and notice nothing happens
The reason for the final behavior is the click event both opens and closes the message, (calls set state twice)
Link to code example:
https://codesandbox.io/s/react-playground-forked-cyt0f?file=/index.js
The current behavior
The expected behavior
That they be consistent