Filter by the role you are applying for, open a question, and practise explaining the answer out loud. Mark it practised when you can.
Why they ask this: Most React bugs (infinite loops, stale data, memory leaks) come from misusing useEffect.
Answer
useEffect(effect, deps) runs effect after render:
[]: runs once after the first render (mount).[a, b]: runs after the first render and again whenever a or b changes (compared with Object.is).The function you return is the cleanup. It runs before the effect runs again and when the component unmounts. Use it to remove listeners, clear timers and cancel requests.
useEffect(() => {
const controller = new AbortController();
fetch(`/api/jobs?q=${query}`, { signal: controller.signal })
.then((r) => r.json())
.then(setJobs)
.catch(() => {});
return () => controller.abort(); // cancel the old request when query changes
}, [query]);Include every value from the component that the effect uses in the array. Missing dependencies cause stale values.