Create a simple timer in React using hooks. It uses the useEffect hook to create the timer and clear it out on exit.
import { useEffect, useState } from "react";
let timer;
export default function Timer()
{
const [time, setTime] = useState(new Date());
useEffect(() =>
{
timer = setInterval(() =>
{
console.log(new Date());
setTime(new Date());
}, 1000);
return () => clearInterval(timer);
}, []);
return (
<div>Time {time.toLocaleString()}</div>
)
}