0
0
Reactframework~10 mins

Cleanup function in React - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Cleanup function
Component mounts
useEffect runs
Setup side effect
Component updates or unmounts?
YesCleanup function runs
Side effect cleaned
Component stays mounted
When a React component mounts, useEffect runs and sets up a side effect. On update or unmount, the cleanup function runs to remove or reset the effect.
Execution Sample
React
useEffect(() => {
  const id = setInterval(() => console.log('tick'), 1000);
  return () => clearInterval(id);
}, []);
This code sets up a timer that logs 'tick' every second and cleans it up when the component unmounts.
Execution Table
StepTriggerActionEffect SetupCleanup Function CalledEffect Cleanup
1Component mountsuseEffect runssetInterval startedNoNo
21 second passesInterval callback runsNo changeNoNo
3Component unmountsCleanup function runsNo changeYesclearInterval called
4After unmountNo further interval callbacksNo changeNoNo
💡 Component unmount triggers cleanup, stopping the interval and preventing further callbacks.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
idundefinedinterval ID assignedinterval ID unchangedinterval clearedundefined or invalid
Key Moments - 3 Insights
Why does the cleanup function run when the component unmounts?
Because React calls the cleanup function returned by useEffect to remove side effects and avoid memory leaks, as shown in step 3 of the execution_table.
What happens if we forget to clear the interval in the cleanup?
The interval keeps running even after the component is gone, causing unexpected behavior or memory leaks, which is prevented by the cleanup in step 3.
Does the cleanup function run on every render?
No, it runs only before the next effect runs or when the component unmounts. Here, with an empty dependency array, it runs only on unmount (step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 3?
AThe component mounts and sets up the interval
BThe cleanup function runs and clears the interval
CThe interval callback logs 'tick'
DNothing happens
💡 Hint
Check the 'Cleanup Function Called' and 'Effect Cleanup' columns at step 3 in the execution_table
According to variable_tracker, what is the state of 'id' after step 3?
AUndefined
BInterval ID assigned and active
CInterval cleared and invalid
DInterval ID changed to a new value
💡 Hint
Look at the 'After Step 3' and 'Final' columns for 'id' in variable_tracker
If the dependency array was omitted, when would the cleanup function run?
AOn every render before the next effect runs
BOnly on component unmount
CNever
DOnly once after the first render
💡 Hint
Recall React's useEffect behavior without dependencies, cleanup runs before each new effect
Concept Snapshot
useEffect(() => {
  // setup side effect
  return () => {
    // cleanup side effect
  };
}, [dependencies]);

Cleanup runs before next effect or on unmount.
Prevents memory leaks and stale effects.
Full Transcript
In React, the cleanup function inside useEffect runs to remove side effects like timers or subscriptions. When the component mounts, useEffect sets up the effect. When the component unmounts or before the effect runs again, React calls the cleanup function to clear or reset the effect. This prevents issues like memory leaks. For example, a timer set with setInterval is cleared in cleanup to stop it when the component is gone. If dependencies are empty, cleanup runs only on unmount. Without dependencies, cleanup runs before every new effect. This flow ensures React components manage resources safely and efficiently.