Course contentsShow
← React
Lesson 543 of 2,39615. useEffect FundamentalsFree lesson

The ESLint Rule: exhaustive-deps

Learn how the eslint-plugin-react-hooks rule catches missing dependencies automatically.

The ESLint Rule: exhaustive-deps

You'll learn how React's official linting rule automatically catches missing dependencies in your useEffect hooks, preventing subtle bugs before they happen.

What is exhaustive-deps?

The exhaustive-deps rule is part of eslint-plugin-react-hooks, a tool that analyzes your useEffect code and warns you when you've forgotten to include a dependency in the dependency array. Think of it as a vigilant assistant that reads your effect code and says, "Hey, you're using that variable inside the effect—shouldn't it be in the dependency array?"

How It Works

When you write an effect, ESLint scans the function body for any props, state, or other values you're referencing. If those values aren't listed in the dependency array, it underlines your code with a warning:

const [count, setCount] = useState(0);
const [name, setName] = useState('');

useEffect(() => {
  console.log(`${name} clicked ${count} times`);
}, [count]); // ⚠️ Warning: name is missing!

The rule knows you're reading name inside the effect, so it expects name in the dependency array. If it's missing, your effect might run with stale data.

Why This Matters

Without this rule, you'd manually track every value your effect uses—and humans forget things. The exhaustive-deps rule catches these mistakes instantly, saving you from bugs where effects don't re-run when they should, or display outdated information.

Key Takeaway: The exhaustive-deps ESLint rule automatically detects when you've forgotten to include a prop, state, or variable in your useEffect dependency array, acting as a safety net against common synchronization bugs.