The unmounting phase is when a React component is removed from the UI and cleaned up before it disappears from the screen.
Click to reveal answer
beginner
Which React hook is used to run cleanup code during the unmounting phase?
The useEffect hook with a cleanup function returned inside it runs during the unmounting phase.
Click to reveal answer
intermediate
Why is cleanup important during the unmounting phase?
Cleanup stops memory leaks and removes event listeners or timers that are no longer needed when the component leaves the screen.
Click to reveal answer
intermediate
Show a simple example of cleanup in a React functional component.
Inside <code>useEffect(() => { const timer = setTimeout(...); return () => clearTimeout(timer); }, []);</code>, the returned function clears the timer when the component unmounts.
Click to reveal answer
intermediate
What happens if you forget to clean up in the unmounting phase?
If cleanup is forgotten, your app may slow down or behave oddly because old timers or listeners keep running even after the component is gone.
Click to reveal answer
Which React hook lets you run code when a component unmounts?
AuseContext
BuseState
CuseEffect with a cleanup function
DuseMemo
✗ Incorrect
The cleanup function returned from useEffect runs when the component unmounts.
What is a common reason to use cleanup in the unmounting phase?
ATo add new components
BTo fetch new data
CTo update the UI
DTo stop timers or remove event listeners
✗ Incorrect
Cleanup stops timers and removes event listeners to prevent memory leaks.
When does the cleanup function inside useEffect run?
AOnly when the component mounts
BBefore the component unmounts or before the effect runs again
CAfter every render
DNever
✗ Incorrect
Cleanup runs before unmounting or before the effect runs again on dependency changes.
What could happen if you don't clean up in the unmounting phase?
AMemory leaks and unexpected behavior
BThe component will never mount
CThe app will crash immediately
DNothing happens
✗ Incorrect
Not cleaning up can cause memory leaks and bugs from leftover processes.
Which of these is NOT a typical cleanup task during unmounting?
AFetching data
BClearing timers
CRemoving event listeners
DCancelling subscriptions
✗ Incorrect
Fetching data is not a cleanup task; it's usually done during mounting or updates.
Explain the unmounting phase in React and why cleanup is important.
Think about what happens when a component leaves the screen.
You got /3 concepts.
Describe how to use the useEffect hook to handle cleanup during unmounting.
Focus on the function returned inside useEffect.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the unmounting phase in React components?
easy
A. To initialize state when the component first appears
B. To update the component when props change
C. To render the component's UI on the screen
D. To clean up resources like timers or event listeners before the component is removed
Solution
Step 1: Understand the unmounting phase
The unmounting phase happens when a component is about to be removed from the screen.
Step 2: Identify the purpose of cleanup
During unmounting, React lets you clean up things like timers or event listeners to avoid bugs and memory leaks.
Final Answer:
To clean up resources like timers or event listeners before the component is removed -> Option D
Quick Check:
Unmounting phase = cleanup before removal [OK]
Hint: Unmounting phase means cleaning up before component disappears [OK]
Common Mistakes:
Confusing unmounting with mounting or updating phases
Thinking unmounting initializes state
Believing unmounting is for rendering UI
2. Which of the following is the correct way to add a cleanup function inside a React useEffect hook?
C. useEffect(() => { () => clearTimeout(timer); }, []);
D. useEffect(() => { return clearTimeout(timer); }, []);
Solution
Step 1: Recall useEffect cleanup syntax
The cleanup function must be returned from the function passed to useEffect.
Step 2: Check each option's syntax
useEffect(() => { return () => { clearTimeout(timer); }; }, []); correctly returns a function that calls clearTimeout. Others either call clearTimeout immediately or do not return a function.
A. The event listener callback should be named function, not arrow function
B. Missing cleanup function to remove the event listener on unmount
C. useEffect should not have an empty dependency array
D. Event listener should be added inside the return statement
Solution
Step 1: Check event listener setup
The component adds a resize event listener on mount but does not remove it on unmount.
Step 2: Identify missing cleanup
Without cleanup, the listener stays active after unmount, causing memory leaks or bugs.
Final Answer:
Missing cleanup function to remove the event listener on unmount -> Option B
Quick Check:
Always remove listeners in cleanup [OK]
Hint: Always remove event listeners in cleanup function [OK]
Common Mistakes:
Forgetting to remove event listeners on unmount
Thinking arrow functions can't be event callbacks
Misusing dependency arrays in useEffect
5. You have a React component that sets up a WebSocket connection in useEffect. Which is the best way to ensure the connection closes properly when the component unmounts?
useEffect(() => {
const socket = new WebSocket('wss://example.com');
socket.onmessage = (event) => console.log(event.data);
// What should go here?
}, []);
hard
A. Return a cleanup function that calls socket.close() to close the connection
B. Call socket.close() immediately after setting onmessage
C. Do nothing; WebSocket closes automatically on unmount
D. Set socket = null in the cleanup function
Solution
Step 1: Understand WebSocket lifecycle in React
WebSocket connections stay open until explicitly closed. They do not close automatically on component unmount.
Step 2: Implement cleanup to close connection
Returning a cleanup function that calls socket.close() ensures the connection closes when the component unmounts, preventing resource leaks.
Final Answer:
Return a cleanup function that calls socket.close() to close the connection -> Option A
Quick Check:
Close WebSocket in cleanup function [OK]
Hint: Close WebSocket inside cleanup function returned by useEffect [OK]
Common Mistakes:
Closing WebSocket immediately instead of on unmount
Assuming WebSocket closes automatically
Setting socket to null without closing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.