The Dependency Array and Execution Lifecycle
The Dependency Array and Execution Lifecycle
1. Controlling Effect Execution
The second argument passed to useEffect is the Dependency Array. This array tells React exactly which state variables or props the effect relies on.
React evaluates the dependency array using shallow equality (Object.is) before every re-render to determine whether the effect callback should run again.
2. The 3 Dependency Array Modes
Mode 1: No Dependency Array (Run on Every Render)
Mode 2: Empty Dependency Array [] (Run on Mount Only)
Mode 3: Specific Dependencies [dep1, dep2] (Run on Change)
3. The Golden Rule of Dependencies
Every reactive value referenced inside your effect callback MUST be included in the dependency array.
A "reactive value" includes:
- Props
- State variables
- Variables or functions declared inside the component body
4. The Pitfall of Objects and Functions in Dependencies
Because React checks dependencies using reference equality (Object.is), defining objects or functions inside your component body can trigger unwanted effect runs:
Practice Quiz
useEffect(() => { ... })?useEffect dependency array?Cleanup Functions in useEffect
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| The useEffect Hook for Side Effects | Cleanup Functions in useEffect |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.