react icon indicating copy to clipboard operation
react copied to clipboard

Bug:

Open kl2400033266 opened this issue 1 month ago • 1 comments

Description

I observed that the cleanup function inside useEffect does not always trigger when there are rapid, consecutive state updates. This leads to stale subscriptions and unexpected behavior in components that rely on cleanup.

Steps to Reproduce

  1. Create a component with a useEffect that logs cleanup.
  2. Trigger state updates inside a loop or with very small intervals (5–10ms).
  3. Observe that cleanup logs are skipped or delayed.

Expected Behavior

Cleanup should run reliably before every re-invocation of the effect, regardless of how fast state is updated.

Actual Behavior

Cleanup is inconsistently triggered during extremely fast re-renders.

Version

  • React: Latest main branch
  • Environment: Browser (Chrome/Edge)
  • OS: Windows 10 / macOS (tested both)

Additional Info

Issue seems more noticeable in strict mode, possibly due to double rendering behavior.

kl2400033266 avatar Nov 26 '25 10:11 kl2400033266

This is intended behavior. React will batch state updates if possible. useEffect only runs when React commits, not when React renders.

eps1lon avatar Nov 26 '25 10:11 eps1lon