saves applications from unwanted behaviours like memory leaks
To avoid race conditions in async requests.
To manage subscriptions in your application
You added a window resize listener inside a useEffect. What steps do you take to make sure it doesn't keep firing after the component unmounts?
If you set a setInterval inside an effect but forget to return a cleanup function, what symptom might you notice when navigating away from the page?
Our component opens a WebSocket connection in useEffect and started receiving duplicate messages after users navigate between routes. How would you debug and fix the issue?
During a performance audit you see memory usage climbing each time a modal opens. The modal adds a CSS class to document.body in an effect. Explain why a cleanup is needed and how you'd implement it.
The dashboard renders dozens of charts, each attaching a ResizeObserver in useEffect. At scale we see jitter and occasional crashes. How would you redesign the cleanup strategy to be safe and performant?
A legacy codebase has many effects without cleanup, leading to stale closures and memory leaks. Describe a systematic approach to refactor these across the repo, considering testing and rollout.
We're migrating from class components with componentWillUnmount to functional components using useEffect. What architectural guidelines would you establish for cleanup to avoid regressions across multiple teams?
We plan to introduce a global event bus that many components subscribe to via useEffect. How would you design the subscription API and cleanup conventions to ensure reliability and ease of maintenance at scale?