0
0
Reactframework~10 mins

Effect execution timing in React - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Effect execution timing
Component Render Start
Evaluate JSX
Commit DOM Updates
Run useEffect Cleanup (if any)
Run useEffect Callback
Wait for next render or unmount
This flow shows how React runs effects after rendering and DOM updates, including cleanup before running new effects.
Execution Sample
React
import React, { useState, useEffect } from 'react';

function Timer() {
  const [count, setCount] = useState(0);
  useEffect(() => {
    const id = setInterval(() => setCount(c => c + 1), 1000);
    return () => clearInterval(id);
  }, []);
  return <div>{count}</div>;
}
This React component uses useEffect to start a timer that increments count every second, cleaning up on unmount.
Execution Table
StepTriggerState BeforeActionState AfterWhat Re-rendersDOM Change
1Initial rendercount=0Render JSX <div>0</div>count=0Timer componentDisplay 0
2Commit phasecount=0Run useEffect callback: start intervalcount=0No re-render yetNo change
3After 1 secondcount=0Interval callback: setCount(1)count=1Timer componentDisplay 1
4Render due to state changecount=1Render JSX <div>1</div>count=1Timer componentUpdate display to 1
5Commit phasecount=1No cleanup needed (first effect run)count=1No re-renderNo change
6After 2 secondscount=1Interval callback: setCount(2)count=2Timer componentDisplay 2
7Render due to state changecount=2Render JSX <div>2</div>count=2Timer componentUpdate display to 2
8Commit phasecount=2No cleanup neededcount=2No re-renderNo change
9Component unmountcount=2Run useEffect cleanup: clearIntervalcount=2Timer component removedRemove display
💡 Component unmount triggers cleanup, stopping the interval and ending updates.
Variable Tracker
VariableStartAfter 1After 2After 3Final
count0123Stopped at unmount
Key Moments - 3 Insights
Why does the effect run after the component renders and not during?
React runs effects after the DOM updates to avoid blocking rendering and to ensure the DOM is ready. See execution_table steps 2 and 5 where effects run after render and commit.
When and why does the cleanup function run?
Cleanup runs before the next effect or on unmount to avoid memory leaks. In the table, cleanup runs at step 9 when the component unmounts.
Why doesn't the effect run on every render in this example?
Because the dependency array is empty ([]), the effect runs only once after the initial render, as shown in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of count at step 4?
A0
B1
C2
DUndefined
💡 Hint
Check the 'State After' column at step 4 in the execution_table.
At which step does the useEffect cleanup function run?
AStep 9
BStep 5
CStep 2
DStep 7
💡 Hint
Look for 'Run useEffect cleanup' in the 'Action' column.
If the dependency array was omitted, how would the effect execution change?
AEffect runs only once after initial render
BEffect never runs
CEffect runs after every render
DEffect runs only on unmount
💡 Hint
Recall how useEffect behaves without dependencies; check the explanation in key_moments.
Concept Snapshot
useEffect runs after render and DOM update.
Cleanup runs before next effect or on unmount.
Empty dependency array means effect runs once.
State changes trigger re-render and possibly effect cleanup.
Effects do not block UI rendering.
Full Transcript
This visual trace shows how React's useEffect hook executes after the component renders and commits DOM updates. Initially, the component renders with count 0. Then the effect callback runs, starting an interval timer. Every second, the interval updates count, causing re-renders. Each re-render updates the displayed count. Cleanup runs only when the component unmounts, stopping the timer. The effect does not run on every render because the dependency array is empty. This ensures effects run at the right time without blocking UI updates.