Effect Cleanup

A clock starts a timer in an effect and stops it in the cleanup. Watch it tick, then hide the clock.

React · beginner · 8 min · 35 steps · free

What you'll learn

EffectCleanup.jsx

import { useState, useEffect } from 'react';

function Clock() {
  const [seconds, setSeconds] = useState(0);

  useEffect(() => {
    const id = setInterval(() => setSeconds((s) => s + 1), 1000);
    console.log('timer started');
    return () => {
      clearInterval(id);
      console.log('timer stopped');
    };
  }, []);

  return <p>{seconds} s</p>;
}

export default function App() {
  const [show, setShow] = useState(true);

  return (
    <div>
      {show && <Clock />}
      <button onClick={() => setShow(!show)}>{show ? 'Hide' : 'Show'} clock</button>
    </div>
  );
}

Step by step

  1. React renders <App> for the first time: it calls the App function.
  2. useState runs for the first time, so React stores the starting value for show and returns it: true.
  3. App returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.
  4. React renders <Clock> for the first time: it calls the Clock function.
  5. useState runs for the first time, so React stores the starting value for seconds and returns it: 0.
  6. useEffect does not run the effect now. React runs it after the page has been updated, and again only when a dependency changes.
  7. Clock returns JSX: a description of what the screen should show, <p>0 s</p>. React has not changed the page yet.
  8. React turns the JSX into real page elements and shows them: look at the Screen panel.
  9. The page is up to date, so React now runs the effect.
  10. setInterval starts a timer that ticks every second; id is its number.
  11. Prints “timer started”.
  12. The effect returns a cleanup function. React will call it before running the effect again, or when Clock is removed.
  13. A second later the timer fires and calls setSeconds with an update function. React works this one out at once: s = 0 → 1.
  14. The timer's callback has queued the update; React renders Clock again.
  15. React renders <Clock> again (render 2): it calls Clock once more, from the top.
  16. useState returns the value React has kept for seconds: 1. The starting value is only used on the first render.
  17. useEffect does not run the effect now. React runs it after the page has been updated, and again only when a dependency changes.
  18. Clock returns JSX: a description of what the screen should show, <p>1 s</p>. React has not changed the page yet.
  19. React compares the new JSX with the last one and updates only what changed on the page.
  20. Another second, another tick.
  21. React renders <Clock> again (render 3): it calls Clock once more, from the top.
  22. React calls the update function with s = 1. It returns 2, the new value.
  23. useState returns the value React has kept for seconds: 2. The starting value is only used on the first render.
  24. useEffect does not run the effect now. React runs it after the page has been updated, and again only when a dependency changes.
  25. Clock returns JSX: a description of what the screen should show, <p>2 s</p>. React has not changed the page yet.
  26. React compares the new JSX with the last one and updates only what changed on the page.
  27. You click Hide clock.
  28. setShow(!show) does not change show right away: React queues the update and will render again. In this call show is still true.
  29. React renders <App> again (render 2): it calls App once more, from the top.
  30. useState returns the value React has kept for show: false. The starting value is only used on the first render.
  31. App returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.
  32. React removes Clock from the page.
  33. Clock is gone, so React calls the cleanup it kept.
  34. clearInterval(id) stops the timer. Nothing will tick any more.
  35. Prints “timer stopped”.

Common mistakes

Best practices

Key takeaways

Check yourself

With [] dependencies, when does the cleanup run?

Answer: When the component is removed

What happens without clearInterval here?

Answer: The timer keeps running after Clock is gone

·

More in Effects and Refs