What is the useEffect hook?
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.
Previous
What is the useState hook?
Next
What is the difference between controlled and uncontrolled components?