0
0
Reactframework~10 mins

Dependency array usage in React - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Dependency array usage
Component renders
Dependency array checked
Dependencies changed?
NoSkip effect
Yes
Effect cleanup runs (if any)
Effect callback executes
Shows how React checks dependencies after each render to decide if useEffect runs or skips.
Execution Sample
React
import React, { useState, useEffect } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  useEffect(() => {
    console.log(`Count is ${count}`);
  }, [count]);
  return <button onClick={() => setCount(count + 1)}>Increment</button>;
}
A counter button that logs the count only when count changes.
Execution Table
StepRender #count valueDependency array [count]Effect runs?Effect output
1Initial render0[0]YesCount is 0
2Click button -> setCount(1)1[1]YesCount is 1
3Click button -> setCount(2)2[2]YesCount is 2
4Render without count change2[2]NoNo effect run
5Click button -> setCount(3)3[3]YesCount is 3
💡 Effect skips when dependency array values do not change between renders.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
count012233
dependency array [count][0][1][2][2][3][3]
effect runs?YesYesYesNoYesYes
Key Moments - 3 Insights
Why does the effect not run on every render?
Because React compares the dependency array values from the last render to the current one. If they are the same (see step 4 in execution_table), it skips running the effect.
What happens if the dependency array is empty?
The effect runs only once after the initial render and never again, since there are no dependencies to change.
Why do we include variables in the dependency array?
To tell React to re-run the effect only when those variables change, avoiding unnecessary effect executions.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which render does the effect NOT run?
ARender 2
BRender 4
CRender 3
DRender 1
💡 Hint
Check the 'Effect runs?' column in execution_table for each render step.
What is the value of count after the third click according to variable_tracker?
A3
B2
C1
D0
💡 Hint
Look at the 'count' row in variable_tracker after 'After 4'.
If we remove 'count' from the dependency array, how would the effect behave?
AIt runs on every render
BIt never runs
CIt runs only once after initial render
DIt runs only when count changes
💡 Hint
Recall how an empty dependency array affects useEffect execution.
Concept Snapshot
useEffect(() => { /* effect */ }, [deps])
- Runs effect after render if any dep changed
- Skips effect if deps unchanged
- Empty array runs effect once
- Helps optimize side effects
- Always include all used variables in deps
Full Transcript
This visual shows how React's useEffect hook uses the dependency array to decide when to run an effect. On each render, React compares the current dependency array values to the previous ones. If any value changed, the effect runs; otherwise, it skips. For example, a counter component logs the count only when the count changes. The dependency array includes 'count', so the effect runs on initial render and whenever count updates. If the dependency array is empty, the effect runs only once after the first render. This mechanism helps avoid unnecessary effect executions and keeps components efficient.