useEffect
In one line
useEffect is a Hook that lets you perform side effects — data fetching, subscriptions, DOM manipulation — after the component renders.
In simple words
useEffect is the Hook that handles side effects: anything that reaches outside the component — fetching data, setting up a subscription, manually changing the DOM, starting a timer, writing to localStorage.
You pass two arguments: a setup function and an optional dependency array. The setup function runs after render. If you return a function from setup, it becomes the cleanup function that runs before the next effect or when the component unmounts.
The dependency array controls when the effect re-runs: [] means run once on mount; [value] means run on mount and whenever value changes; omitting the array means run after every render. Getting the dependency array right is one of the most common sources of bugs in React.
Example
useEffect(() => {
document.title = `Count: ${count}`;
}, [count]);
// Runs on mount and whenever count changes.The effect updates the document title whenever count changes. The dependency array [count] tells React to re-run the effect only when count changes.
Common confusions
Confused with: useLayoutEffect
The difference: useEffect runs asynchronously after paint, so it won't block the browser. useLayoutEffect runs synchronously after DOM mutations but before paint — use it only when you need to measure or modify the DOM before the user sees it.
Related terms
Related Resources
Related Lessons
- useEffect
Learn useEffect, dependency arrays, and cleanup.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.