What is the useEffect hook?
Why Interviewers Ask This
Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for React.js development. It reveals whether you understand the building blocks that more complex concepts rely on.
Answer
useEffect is a React hook for running side effects in function components — operations that interact with systems outside React (network requests, subscriptions, timers, direct DOM manipulation, logging). Signature: useEffect(setup, dependencies?). When it runs: after every render (no dependency array); once after the first render (empty array []); when specific values change ([dep1, dep2]). Cleanup: return a function from the effect to clean up — called before the next effect runs and when the component unmounts: useEffect(() => { const sub = subscribe(id); return () => sub.unsubscribe(); }, [id]);. Common mistakes: missing dependencies (stale closure), putting functions in the dependency array that are recreated every render, running effects without cleanup (memory leaks). In React 18 with Strict Mode, effects run twice in development to surface cleanup bugs. Note: useEffect is being gradually supplemented by purpose-built hooks like useSyncExternalStore for subscriptions and use() for async data in future React versions.
Common Mistake
Don't just define the term — demonstrate that you understand when to use it and when not to. Showing awareness of trade-offs is what separates average from strong React.js candidates.
Previous
What is the useState hook?
Next
What is the difference between controlled and uncontrolled components?