Course contentsShow
← React
Lesson 1020 of 2,39626. Rules of Hooks Deep DiveFree lesson

The exhaustive-deps Rule

Validates dependency arrays for useEffect, useMemo, and useCallback to prevent stale closures and missing deps.

The exhaustive-deps Rule

What You'll Learn

You'll learn how the exhaustive-deps ESLint rule catches missing or unnecessary dependencies in your effect and memoization hooks to prevent bugs.

Why This Rule Exists

When you use useEffect, useMemo, or useCallback, you provide a dependency array that tells React when to re-run that code. But here's the problem: it's incredibly easy to forget to include a value you're using inside the hook, or to include values that aren't actually needed.

Stale closures happen when your effect captures an old version of a variable because you didn't list it as a dependency. The exhaustive-deps rule analyzes your hook's body and warns you when the dependency array doesn't match what you're actually using.

How It Works

The rule examines every variable, prop, or state value referenced inside your useEffect, useMemo, or useCallback. If you reference something that comes from outside the hook but don't list it in the dependency array, you get a warning.

// ❌ ESLint will warn: 'count' is missing from deps
useEffect(() => {
  document.title = `Count: ${count}`;
}, []); // Empty array, but count is used!

// ✅ Fixed: count is now included
useEffect(() => {
  document.title = `Count: ${count}`;
}, [count]);

The rule also catches unnecessary dependencies—values included in the array but never used in the hook body.

The Safety Net

Think of this rule as a spell-checker for your dependency arrays. Just like a spell-checker prevents typos from slipping through, exhaustive-deps prevents stale closure bugs from reaching production. It's not perfect, but it catches 95% of dependency mistakes automatically.

Key Takeaway: The exhaustive-deps rule is your safety net against stale closures and incorrect dependency arrays—trust its warnings and fix them rather than disabling the rule.