Dependency Array
In one line
The dependency array is the second argument to useEffect/useMemo/useCallback that tells React when to re-run or recompute.
In simple words
The dependency array controls when a Hook re-runs. It's the second argument to useEffect, useMemo, and useCallback — an array of values that the Hook depends on.
[]— run once on mount (for effects) / compute once (for memo). The empty array means "no dependencies, never re-run."[value]— run on mount and whenevervaluechanges.[a, b]— run on mount and wheneveraorbchanges.- Omitted — run after every render (effects only; useMemo/useCallback require a deps array).
Getting the dependency array right is critical. If you omit a dependency that the effect uses, you get stale closures (the effect uses old values). If you include too many, the effect runs more than necessary. The eslint-plugin-react-hooks exhaustive-deps rule catches these issues automatically.
Example
useEffect(() => {
document.title = `Hello, ${name}`;
}, [name]); // re-run only when name changes
useEffect(() => {
console.log("Mounted!");
}, []); // run once on mountThe first effect re-runs whenever name changes. The second runs only once because the dependency array is empty.
Common confusions
Confused with: empty array
The difference: An empty dependency array [] means 'run once on mount.' Omitting the array entirely means 'run after every render.' These are very different — omitting it is rarely what you want.
Related terms
Related Resources
Related Lessons
- useEffect
Learn dependency arrays and common pitfalls.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.