0
0
Reactframework~10 mins

Lifecycle mapping with hooks in React - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Lifecycle mapping with hooks
Component Mounts
Initial Render Complete
useEffect(() => { ... }, [
State or Props Change
Component Re-renders
useEffect(() => { ... }, [deps
Component Unmounts
Cleanup Runs
This flow shows how React functional components use hooks to run code when mounting, updating, and unmounting.
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 Timer component increases count every second using useEffect to set up and clean up the interval.
Execution Table
StepTriggerState BeforeActionState AfterWhat Re-rendersDOM Change
1Component mountscount: undefinedInitialize count=0count: 0Timer componentRender <div>0</div>
2useEffect runs after mountcount: 0Set interval to increment count every 1scount: 0No immediate re-renderNo DOM change
31 second passescount: 0setCount increments count to 1count: 1Timer componentUpdate <div>1</div>
41 second passescount: 1setCount increments count to 2count: 2Timer componentUpdate <div>2</div>
5Component unmountscount: 2Cleanup: clearInterval calledcount: 2No re-renderInterval stops, DOM remains <div>2</div>
💡 Component unmounts, cleanup runs, interval stops, no further updates.
Variable Tracker
VariableStartAfter 1After 2After 3Final
countundefined0122
intervalIdundefinedid setid setid setcleared
Key Moments - 3 Insights
Why does useEffect run after the first render and not during it?
useEffect runs after the render to avoid blocking UI updates. See execution_table step 2 where effect sets interval only after initial render.
What happens if we forget to return a cleanup function in useEffect?
The interval would keep running even after unmount, causing memory leaks. Step 5 shows cleanup stopping the interval.
Why does the component re-render when count changes?
Calling setCount updates state, triggering React to re-render the component with new count. See steps 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of count at Step 3?
A1
B2
C0
Dundefined
💡 Hint
Check the 'State After' column at Step 3 in execution_table.
At which step does the cleanup function run?
AStep 3
BStep 5
CStep 2
DStep 1
💡 Hint
Look for 'Component unmounts' and 'Cleanup' in execution_table.
If we remove the empty dependency array in useEffect, what changes in the execution?
AEffect runs only once after mount
BCleanup never runs
CEffect runs after every render causing multiple intervals
DState never updates
💡 Hint
Dependency array controls when useEffect runs; see step 2 explanation.
Concept Snapshot
React functional components use useEffect hook to run code at key lifecycle points.
- useEffect with [] runs once after mount.
- Returning a function cleans up on unmount.
- State changes trigger re-renders.
- Effects run after render to keep UI responsive.
Full Transcript
This visual execution shows how React functional components use hooks to manage lifecycle events. When the component mounts, useState initializes state. Then useEffect runs after the first render to set up side effects like intervals. Each state update triggers a re-render, updating the DOM. When the component unmounts, the cleanup function runs to clear intervals and prevent leaks. This flow helps beginners see how hooks replace class lifecycle methods with simple, clear steps.