Skip to content

QuizNot attempted

Empty dependency array: mount and unmount

The button is clicked 3 times, then the component unmounts. What logs, in order?
function App() {
const [count, setCount] = useState(0);
useEffect(() => {
console.log('mounted');
return () => console.log('cleanup');
}, []);
return <button onClick={() => setCount(c => c + 1)}>{count}</button>;
}
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • mounted (once) → cleanup (once, on unmount)
  • With [] the effect has no dependencies to change, so it runs once after mount and its cleanup runs once on unmount
  • clicks re-render but never re-run it.
  • Not mounted/cleanup per click: that is what a [count] dependency would do.
  • Not 'mounted → cleanup' immediately: cleanup waits for unmount (StrictMode dev adds an extra mount/cleanup, but that is a dev check).
  • Not 'three mounts, no cleanup': the effect doesn't re-run, and the returned cleanup always runs on unmount.