0
0
Reactframework~20 mins

Unmounting phase in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Unmounting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a React component unmounts?
Consider a React functional component that uses useEffect with a cleanup function. What is the behavior of the cleanup function during the unmounting phase?
React
import React, { useEffect } from 'react';

function Timer() {
  useEffect(() => {
    const id = setInterval(() => console.log('tick'), 1000);
    return () => clearInterval(id);
  }, []);

  return <div>Timer running</div>;
}
AThe cleanup function runs once when the component unmounts, stopping the interval.
BThe cleanup function never runs because the dependency array is empty.
CThe cleanup function runs every second along with the interval callback.
DThe cleanup function runs only when the component re-renders, not on unmount.
Attempts:
2 left
💡 Hint
Think about what happens to side effects when a component is removed from the screen.
lifecycle
intermediate
1:30remaining
Which React hook is best for cleanup on unmount?
You want to perform cleanup logic when a functional component unmounts. Which hook and pattern should you use?
AuseState to track unmount and run cleanup in the setter.
BuseEffect with a return function and an empty dependency array.
CuseMemo to memoize cleanup logic on unmount.
DuseCallback to define cleanup and call it on unmount.
Attempts:
2 left
💡 Hint
Cleanup logic is usually inside a return function of a hook that runs effects.
🔧 Debug
advanced
2:30remaining
Why does this cleanup not run on unmount?
Identify why the cleanup function does not run when the component unmounts in this code:
React
import React, { useEffect } from 'react';

function Example() {
  useEffect(() => {
    const id = setTimeout(() => console.log('done'), 1000);
  }, []);

  return <div>Example</div>;
}
ABecause the dependency array is empty, cleanup runs only on re-render.
BBecause setTimeout does not need cleanup and React ignores it.
CBecause the cleanup function is missing; no function is returned from useEffect.
DBecause the component never unmounts, so cleanup is not triggered.
Attempts:
2 left
💡 Hint
Check if useEffect returns a function for cleanup.
state_output
advanced
2:00remaining
What is the console output when a component unmounts?
Given this React component, what will be logged to the console when the component unmounts?
React
import React, { useEffect } from 'react';

function Logger() {
  useEffect(() => {
    console.log('Mounted');
    return () => console.log('Unmounted');
  }, []);

  return <div>Logger</div>;
}
A"Mounted" logs on mount, "Unmounted" logs on unmount.
B"Unmounted" logs on mount, "Mounted" logs on unmount.
COnly "Mounted" logs, no unmount log appears.
DNo logs appear because useEffect has an empty dependency array.
Attempts:
2 left
💡 Hint
Remember the order of useEffect and its cleanup function.
🧠 Conceptual
expert
3:00remaining
Why is cleanup important in React unmounting phase?
Why should you always include cleanup logic in useEffect when your component sets up subscriptions or timers?
ATo ensure the component never unmounts accidentally.
BTo speed up the initial render of the component.
CTo automatically re-run effects on every render.
DTo prevent memory leaks and unexpected behavior after the component is removed.
Attempts:
2 left
💡 Hint
Think about what happens if timers or subscriptions keep running after the component is gone.