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

Fixing Missing Dependencies: Add Them

Learn the first strategy—just add the missing value to the dependency array as ESLint suggests.

Fixing Missing Dependencies: Add Them

You'll learn the simplest and most common way to fix ESLint's "missing dependency" warning: just add the value to your dependency array.

The Problem Recap

Remember that ESLint's exhaustive-deps rule warns you when your effect reads a value (prop, state, or variable) but doesn't list it in the dependency array. This creates a bug where your effect uses stale data.

The First Strategy: Just Add It

The fix is usually straightforward: take the value ESLint is complaining about and add it to your dependency array.

// ❌ ESLint warning: missing dependency 'count'
useEffect(() => {
  document.title = `Count: ${count}`;
}, []); // Empty array ignores count changes

// ✅ Fixed: count is now a dependency
useEffect(() => {
  document.title = `Count: ${count}`;
}, [count]); // Effect re-runs whenever count changes

Why This Works

When you add the missing value to the array, React will re-run your effect whenever that value changes. Your effect now stays synchronized with the latest data—no more stale values.

Think of it like a subscription list: if your effect "reads" a magazine (uses a value), it needs to "subscribe" to that magazine (include it in dependencies) to get the latest issues (updated values).

The Default Approach

In most cases, this is exactly what you want to do. ESLint is telling you: "Your effect uses this value, so it should re-run when this value changes." Trust the linter—it's preventing bugs.

Key Takeaway: When ESLint warns about a missing dependency, your first move should be to add that dependency to the array—it's the simplest fix and usually the correct one.