react
react copied to clipboard
Bug:
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
- Create a component with a useEffect that logs cleanup.
- Trigger state updates inside a loop or with very small intervals (5–10ms).
- 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.
This is intended behavior. React will batch state updates if possible. useEffect only runs when React commits, not when React renders.