The eslint-plugin-react-hooks Exhaustive-Deps Rule
You'll learn how ESLint automatically catches missing dependencies in your memoization hooks and why you should trust its warnings.
What This Rule Does
The exhaustive-deps rule from eslint-plugin-react-hooks analyzes your useMemo, useCallback, and useEffect hooks and warns you when you've forgotten to include a dependency in the array. It's like having a vigilant code reviewer who never gets tired of checking every hook you write.
When you reference a variable inside your memoization function but don't list it in the dependency array, ESLint will show a warning:
const memoizedValue = useMemo(() => {
return expensiveCalculation(count, multiplier);
}, [count]); // ⚠️ React Hook useMemo has a missing dependency: 'multiplier'
Why This Rule Exists
This rule prevents stale closures—situations where your memoized function captures old values because React doesn't know it should recompute. Without the warning, you might ship bugs where your UI displays outdated data or your callback operates on stale state.
Think of it like a car's check-engine light. It's annoying when it comes on, but it's almost always pointing to a real problem that will cause issues later.
Why You Shouldn't Disable It
Some developers add // eslint-disable-next-line to silence the warning when they "know better." This is almost always wrong. If ESLint says you're missing a dependency, you probably are. The correct fixes are:
- Add the missing dependency
- Extract primitive values instead of referencing entire objects
- Move the computation outside the component if it doesn't depend on props/state
Key Takeaway: The exhaustive-deps rule catches real bugs before they reach production. Trust the warning—it's teaching you to write correct React code.