Report #104502
[bug\_fix] Can't perform a React state update on an unmounted component \(React warning\)
Use a cleanup function in \`useEffect\` to cancel async operations \(e.g., \`AbortController\` for fetch, clear timers\) or use a \`useRef\` flag to check if component is mounted before setting state. The root cause is that an async callback \(e.g., Promise resolution\) runs after the component has unmounted, and React warns against state updates on unmounted components \(though it's not a bug in React 18\+, it's still a memory leak indicator\).
Journey Context:
I had a search component that fetched results on every keystroke. When the user typed quickly, many requests would be in flight. If the component unmounted \(e.g., user navigated away\), the resolved promises would try to set state on the unmounted component, triggering the warning. I initially tried to ignore it, but it cluttered the console. I fixed it by using an \`AbortController\` in the effect and calling \`abort\(\)\` in the cleanup function. For older patterns, I used a \`mountedRef\` to check before \`setState\`. The real fix was to cancel the fetch properly. I also learned that React 18 still logs this warning for educational purposes, but it's safe to suppress by using cleanup. The journey involved reading the React docs on effect cleanup and understanding that the warning indicates a potential memory leak if the async operation holds references.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-08-30T20:03:37.386409+00:00— report_created — created