0
0
Reactframework~10 mins

Unmounting phase in React - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Unmounting phase
Component Mounted
User triggers unmount
React calls cleanup functions
Component DOM removed
Component fully unmounted
This flow shows how React removes a component: it first calls cleanup code, then removes the component from the page.
Execution Sample
React
import React, { useEffect } from 'react';

function Timer() {
  useEffect(() => {
    const id = setInterval(() => console.log('tick'), 1000);
    return () => clearInterval(id);
  }, []);
  return <div>Timer running</div>;
}
This component logs 'tick' every second and cleans up the timer when it unmounts.
Execution Table
StepTriggerState BeforeActionState AfterWhat Re-rendersDOM Change
1Component mountsNo timer runningSet interval timerTimer runningTimer componentTimer div added
2Interval firesTimer runningConsole logs 'tick'Timer runningNo re-renderNo DOM change
3User triggers unmountTimer runningCall cleanup: clearIntervalTimer stoppedNo re-renderTimer div removed
4After unmountTimer stoppedNo actionNo componentNo re-renderNo DOM element
💡 Component unmounted, cleanup done, no more timer or DOM element
Variable Tracker
VariableStartAfter 1After 2After 3Final
id (timer ID)undefinedsetInterval IDsetInterval IDclearedcleared
Component mountedfalsetruetruefalsefalse
Key Moments - 2 Insights
Why do we return a function inside useEffect?
The returned function is React's way to run cleanup before unmounting, as shown in step 3 of the execution_table where clearInterval is called.
Does the component re-render when it unmounts?
No, unmounting removes the component from the DOM without re-rendering, as seen in step 3 and 4 where no re-render happens.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 3?
AThe interval timer is set
BThe component re-renders
CThe cleanup function clears the interval and component unmounts
DNothing happens
💡 Hint
Check the 'Action' and 'DOM Change' columns at step 3 in execution_table
According to variable_tracker, what is the state of 'id' after step 3?
Acleared (timer stopped)
BsetInterval ID active
Cundefined
Dnull
💡 Hint
Look at the 'id (timer ID)' row after 'After 3' column in variable_tracker
If the cleanup function was missing, what would happen when the component unmounts?
AReact would automatically clear the timer
BThe timer would keep running causing potential bugs
CThe component would not unmount
DNothing changes
💡 Hint
Think about the purpose of the cleanup function shown in step 3 of execution_table
Concept Snapshot
Unmounting phase in React:
- Happens when component is removed from UI
- useEffect cleanup function runs here
- Cleanup stops timers, subscriptions, etc.
- No re-render during unmount
- Prevents memory leaks and bugs
Full Transcript
In React, the unmounting phase occurs when a component is removed from the screen. React first runs any cleanup functions returned by useEffect hooks. For example, if a timer was set, the cleanup function clears it. Then React removes the component's DOM elements. This prevents leftover processes and memory leaks. The component does not re-render during unmounting. This flow ensures clean removal of components and their side effects.