Course contentsShow
← React
Lesson 613 of 2,39617. Common useEffect MistakesFree lesson

The eslint-plugin-react-hooks/exhaustive-deps Rule

This ESLint rule warns you when your effect uses values that aren't listed in the dependency array.

The eslint-plugin-react-hooks/exhaustive-deps Rule

What you'll learn: How ESLint automatically catches missing dependencies in your useEffect hooks to prevent subtle bugs.

Your Automated Safety Net

Remember learning that missing dependencies cause bugs? The eslint-plugin-react-hooks/exhaustive-deps rule is your automated assistant that warns you whenever you use a value inside your effect that isn't listed in the dependency array.

This ESLint rule analyzes your effect code and checks: "Are you reading any props, state, or other variables? If so, are they ALL in the dependency array?" If not, you get a warning.

Why This Matters

Without this rule, you might write:

function SearchResults({ query }) {
  const [results, setResults] = useState([]);

  useEffect(() => {
    fetchResults(query).then(setResults);
  }, []); // ESLint warning: React Hook useEffect has a missing dependency: 'query'

  return <div>{results.length} results</div>;
}

The rule immediately tells you: "Hey, you're using query inside the effect, but it's not in the dependency array. This will cause a bug—your effect won't re-run when query changes!"

Think of It as Spell-Check

Just like spell-check underlines misspelled words, this ESLint rule underlines missing dependencies. It doesn't fix the problem for you, but it catches mistakes before they become bugs in production.

The rule appears as a yellow squiggly line in your editor (if configured) or as a warning when running ESLint. It's been teaching you throughout the earlier lessons about fixing dependencies—you've likely seen its warnings already!

Key Takeaway: The eslint-plugin-react-hooks/exhaustive-deps ESLint rule automatically detects when you've forgotten to include a dependency in your effect's array, acting as a safety net that prevents stale closure bugs before they reach production.