Consider this React component using useEffect with a cleanup function. What will be logged when the component unmounts?
import React, { useEffect } from 'react'; function Timer() { useEffect(() => { const id = setInterval(() => { console.log('Tick'); }, 1000); return () => { clearInterval(id); console.log('Cleanup done'); }; }, []); return <div>Timer running</div>; }
Think about what useEffect cleanup functions do when the component unmounts.
The cleanup function returned by useEffect runs once when the component unmounts. It clears the interval and logs "Cleanup done". The interval logs "Tick" every second while mounted.
Given this React component, when will the cleanup function inside useEffect run?
import React, { useState, useEffect } from 'react'; function Counter() { const [count, setCount] = useState(0); useEffect(() => { console.log('Effect runs'); return () => { console.log('Cleanup runs'); }; }, [count]); return <button onClick={() => setCount(count + 1)}>Count: {count}</button>; }
Remember how useEffect cleanup works with dependencies.
The cleanup function runs before the effect runs again due to dependency changes and also when the component unmounts.
Examine this React component. Why does it cause a memory leak despite having a cleanup function?
import React, { useEffect } from 'react'; function ScrollTracker() { useEffect(() => { const onScroll = () => { console.log(window.scrollY); }; window.addEventListener('scroll', onScroll); return () => { // Missing cleanup of event listener }; }, []); return <div style={{ height: '200vh' }}>Scroll down</div>; }
Think about what should happen inside the cleanup function for event listeners.
The cleanup function must remove the event listener added in the effect. Here it is empty, so the listener stays active causing a memory leak.
Choose the correct syntax for returning a cleanup function inside useEffect in React.
import React, { useEffect } from 'react'; function Example() { useEffect(() => { const timer = setTimeout(() => console.log('Hello'), 1000); // Which return is correct? }, []); return <div>Example</div>; }
The cleanup function must be a function returned from the effect.
The cleanup function is a function returned from useEffect. Option A correctly returns an arrow function that calls clearTimeout.
Analyze this React component. What will be the exact console output sequence when it mounts and then unmounts?
import React, { useState, useEffect } from 'react'; function Demo() { const [count, setCount] = useState(0); useEffect(() => { console.log('Effect start', count); return () => { console.log('Cleanup', count); }; }, [count]); useEffect(() => { console.log('Mount only'); return () => { console.log('Unmount only'); }; }, []); return <button onClick={() => setCount(c => c + 1)}>Count: {count}</button>; }
Consider the order of effects and cleanups when dependencies change and when the component unmounts.
The first effect with [count] runs on mount with count 0, then cleanup runs before next effect runs on count change. The second effect with empty dependencies runs once on mount and cleanup on unmount.