Fundamentals5 / 5
Up next

useEffect

import { useEffect, useState } from "react";

function Clock() {
  const [now, setNow] = useState(Date.now());
  useEffect(() => {
    const id = setInterval(() => setNow(Date.now()), 1000);
    return () => clearInterval(id);
  }, []);
  return <span>{now}</span>;
}
What you're solving

an interval with cleanup