Problem Statement
How do you perform cleanup in async calls to avoid memory leaks?
Explanation
When a component unmounts during an async call, you should cancel ongoing operations. This can be done with AbortController, cleanup functions in useEffect, or checking mounted flags before updating state.
Code Solution
SolutionRead Only
useEffect(() => {
const controller = new AbortController();
fetch('/api', { signal: controller.signal });
return () => controller.abort();
}, []);Practice Sets
This question appears in the following practice sets: