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

What the Dependency Array Actually Does

The dependency array tells React when to re-run your effect by comparing values between renders.

What the Dependency Array Actually Does

You'll learn how React uses the dependency array to decide whether your effect needs to run again.

The Core Mechanism

The dependency array is React's comparison checklist. Between renders, React looks at each value in your dependency array and checks: "Did this value change?" If any value is different from the previous render, React re-runs your effect. If all values are the same, React skips the effect entirely.

Think of it like a security system checking multiple sensors. The alarm (your effect) only triggers if at least one sensor detects a change.

useEffect(() => {
  console.log(`User ${userId} logged in`);
}, [userId]); // React compares userId on every render

Here, React stores userId from the previous render. On the next render, if userId is still 42, the effect doesn't run. But if userId changes to 99, React sees the difference and executes the effect.

How React Compares Values

React uses Object.is() comparison (similar to ===). For primitives like numbers, strings, and booleans, this means comparing the actual value. For objects and arrays, it compares references — two objects with identical contents but different memory locations are considered different.

This comparison happens automatically before every render where the component updates.

The Decision Flow

  1. Component renders
  2. React checks: "Does this component have effects with dependency arrays?"
  3. For each effect: "Did any dependency change since last time?"
  4. If yes → run the effect (and cleanup from last time first)
  5. If no → skip this effect entirely

Key Takeaway: The dependency array isn't about what your effect needs—it's React's trigger mechanism for when to re-synchronize your effect with the current props and state.