0
0
Reactframework~10 mins

Common lifecycle use cases in React - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Common lifecycle use cases
Component Mounts
useEffect with [
Component Renders
User Interaction or Prop Change
State Update triggers Re-render
useEffect with dependencies runs
Component Unmounts
Cleanup function runs
This flow shows how React functional components use useEffect for mounting, updating, and cleanup during their lifecycle.
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>Seconds: {count}</div>;
}
A timer component that increments seconds every second using useEffect to start and clean up the interval.
Execution Table
StepTriggerState BeforeActionState AfterWhat Re-rendersDOM Change
1Initial Mountcount=0Run useEffect (start interval)count=0Timer componentDisplay 'Seconds: 0'
21 second passescount=0Interval callback increments countcount=1Timer componentDisplay 'Seconds: 1'
32 seconds passcount=1Interval callback increments countcount=2Timer componentDisplay 'Seconds: 2'
4Component Unmountscount=2Cleanup function clears intervalcount=2No re-renderTimer removed from DOM
5No further intervalscount=2No actioncount=2No re-renderNo DOM change
💡 Component unmounts and cleanup stops interval, no further state updates.
Variable Tracker
VariableStartAfter 1After 2After 3Final
count01222
Key Moments - 3 Insights
Why does the interval keep running even though the component re-renders multiple times?
Because the useEffect has an empty dependency array [], it runs only once on mount, so the interval is set up once and keeps running independently of re-renders (see execution_table steps 1-3).
What happens if we forget to return the cleanup function in useEffect?
The interval would continue running even after the component unmounts, causing memory leaks and unexpected behavior (see execution_table step 4 for cleanup importance).
Why does the component re-render when count changes?
Because setCount updates state, React triggers a re-render to update the displayed count (see execution_table steps 2 and 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of count at step 3?
A0
B1
C2
D3
💡 Hint
Check the 'State After' column at step 3 in the execution_table.
At which step does the cleanup function run to stop the interval?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for 'Cleanup function clears interval' in the 'Action' column.
If the dependency array in useEffect was omitted, how would the execution table change?
AThe interval would start only once on mount.
BThe interval would restart on every render, creating multiple intervals.
CThe cleanup would never run.
DThe count would never update.
💡 Hint
Think about how useEffect runs without dependencies and how that affects intervals.
Concept Snapshot
React functional components use useEffect to handle lifecycle events.
- useEffect with [] runs once on mount (setup).
- Return a cleanup function to run on unmount.
- State updates trigger re-renders.
- useEffect with dependencies runs on those changes.
This manages side effects like timers safely.
Full Transcript
This visual execution shows how React functional components use the useEffect hook to manage common lifecycle use cases. When the component mounts, useEffect runs once to start a timer interval. The state variable 'count' increments every second, triggering re-renders that update the displayed seconds. When the component unmounts, the cleanup function returned by useEffect clears the interval to prevent memory leaks. The execution table traces each step: initial mount, timer ticks, and cleanup on unmount. The variable tracker shows how 'count' changes over time. Key moments clarify why useEffect runs only once with an empty dependency array, why cleanup is necessary, and how state updates cause re-renders. The quiz tests understanding of state values at steps, when cleanup runs, and effects of missing dependencies. This helps beginners see React lifecycle in action with hooks.