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: Re-rendering nested components breaks event listeners on parent #27618

Open
greebl3 opened this issue Oct 28, 2023 · 9 comments
Open

Bug: Re-rendering nested components breaks event listeners on parent #27618

greebl3 opened this issue Oct 28, 2023 · 9 comments
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@greebl3
Copy link

greebl3 commented Oct 28, 2023

Re-rendering nested components breaks event-handlers; wrapping handler functions in useCallback makes no difference

React version: 18.2

Link to code example: https://codesandbox.io/s/re-rendering-breaks-event-listener-2dxjhz

Note: The repro makes use of Touch events, so open the Sandbox link on a mobile device

The current behavior

onTouchEnd does not fire when re-rendering a nested component, even when caching handler-functions with useCallback

The expected behavior

Re-rendering a nested component should have no effect on its parent's event-listeners

@greebl3 greebl3 added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Oct 28, 2023
@greebl3 greebl3 closed this as completed Oct 28, 2023
@greebl3 greebl3 reopened this Oct 29, 2023
@greebl3 greebl3 changed the title Bug: Re-renders break onTouchStart handlers Oct 29, 2023
@greebl3
Copy link
Author

greebl3 commented Oct 29, 2023

Figured out the issue is specific to re-rendering nested components. Still seems like unexpected behavior, so I'm reopening the issue with appropriate edits.

Definitely possible that I'm overlooking something obvious, so please feel free to reply with your thoughts

@MarceloSpessoto
Copy link

Hello @greebl3 , looks like the onTouchEnd event listener breaks as soon as the nested component is re-rendered for the first time.
I agree with you that this behavior is unexpected and I'll try to check how to deal with that. Have you found any solution/extra insight during this week?

@greebl3
Copy link
Author

greebl3 commented Nov 4, 2023

Thanks for looking into it @MarceloSpessoto. I feel like this is not a niche issue and/or has implications on a lot of production builds so it would be nice to get more visibility on this.

As a stop-gap, I wrapped the extracted child component in a useCallback. I'll be sure to update this thread when I get the chance to do more digging.

@sweetliquid
Copy link
Contributor

A more visual example: https://codesandbox.io/p/sandbox/re-rendering-breaks-event-listener-forked-p5kqck

See the console log:

mounted
	start
unmounted
mounted
	start
unmounted
mounted
	start
unmounted
mounted
	start

onMouseEnter is called repeatedly on every time the button is mounted.

@sweetliquid
Copy link
Contributor

It doesn't look like onTouchEnd isn't being triggered, but onTouchStart is being called multiple times.

In the example you provided, calling onTouchStart multiple times setup multiple interval timers, but ultimately onTouchEnd is only called once, and remaining interval times is leaked.

@sweetliquid
Copy link
Contributor

I can find time to investigate this issue in the evenings of these few days.

@syd-shields
Copy link

syd-shields commented Mar 31, 2024

Encountering a similar issue (will be opening an issue). Event handlers across the entire window break when re-rendering a nested conditional component. (Edited)

Copy link

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

@github-actions github-actions bot added the Resolution: Stale Automatically closed due to inactivity label Jun 29, 2024
@greebl3
Copy link
Author

greebl3 commented Jun 30, 2024

bumping

@github-actions github-actions bot removed the Resolution: Stale Automatically closed due to inactivity label Jun 30, 2024
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
4 participants