What is a cleanup function in React's useEffect hook?
A cleanup function is a function returned from useEffect that React runs before the component unmounts or before the effect runs again. It helps to clean up resources like timers or subscriptions to avoid memory leaks.
Click to reveal answer
beginner
When is the cleanup function inside useEffect called?
The cleanup function runs right before the component unmounts and also before the effect runs again if the dependencies change.
Click to reveal answer
beginner
Why should you use a cleanup function in React effects?
To stop ongoing processes like timers, cancel subscriptions, or remove event listeners. This prevents bugs and memory leaks when components update or disappear.
Click to reveal answer
intermediate
Show a simple example of a cleanup function in useEffect that clears a timer.
D. useEffect(() => { return clearTimeout(timer); }, []);
Solution
Step 1: Recognize cleanup function syntax
The cleanup function must be returned from the effect callback as a function.
Step 2: Check each option
useEffect(() => { return () => { clearTimeout(timer); }; }, []); correctly returns a function that calls clearTimeout. Others either don't return a function or return the wrong value.
A. The cleanup function should be a function returned, not the result of removeEventListener call
B. The event listener should be added inside the cleanup function
C. The effect should not have a dependency array
D. handleResize should be defined inside useEffect
Solution
Step 1: Check cleanup function syntax
The cleanup must be a function returned from useEffect, not the result of calling removeEventListener immediately.
Step 2: Identify correct cleanup pattern
The cleanup should be: return () => window.removeEventListener('resize', handleResize); to run on unmount.
Final Answer:
The cleanup function should be a function returned, not the result of removeEventListener call -> Option A
Quick Check:
Cleanup must be a returned function [OK]
Hint: Return a function for cleanup, not the cleanup call itself [OK]
Common Mistakes:
Calling cleanup immediately instead of returning a function
Adding event listener inside cleanup
Ignoring dependency array effects
5. You want to subscribe to a WebSocket in a React component and clean up properly. Which code correctly uses a cleanup function to close the socket when the component unmounts or the URL changes?
function SocketComponent({ url }) {
React.useEffect(() => {
const socket = new WebSocket(url);
socket.onmessage = (event) => console.log(event.data);
// What cleanup code is correct?
}, [url]);
return null;
}
hard
A. return () => { socket.close(); };
B. socket.close();
C. return socket.close();
D. return () => socket.onmessage = null;
Solution
Step 1: Understand cleanup for WebSocket
To avoid leaks, the socket must be closed when the component unmounts or URL changes.
Step 2: Identify correct cleanup syntax
The cleanup must be a function returned from useEffect that calls socket.close(). return () => { socket.close(); }; does this correctly.
Final Answer:
return () => { socket.close(); }; -> Option A
Quick Check:
Cleanup returns function closing socket [OK]
Hint: Return a function that closes socket for cleanup [OK]
Common Mistakes:
Calling close immediately instead of returning a function
Returning the result of close() instead of a function
Only nullifying event handlers without closing socket
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.