import React, { useEffect, useState } from "react"; const useTimer = (time ? : number): [{ toggle: () => void; reset: () => void; seconds: number; isActive: boolean; isPaused: boolean; setIsActive: React.Dispatch>; setIsPaused: React.Dispatch>; setSeconds: React.Dispatch>; }] => { const [seconds, setSeconds] = useState(0); const [isActive, setIsActive] = useState(true); const [isPaused, setIsPaused] = useState(false); const toggle = (): void => { setIsActive(!isActive); }; const reset = (): void => { setSeconds(0); }; useEffect(() => { var interval: NodeJS.Timer | undefined = undefined; if (isActive && !isPaused) { interval = setInterval(() => { setSeconds((seconds) => seconds + 1); }, time ? time : 1000); if (seconds !== 0) { clearInterval(interval); } } }, [isActive, seconds, isPaused]); return [{ toggle, reset, seconds, isActive, isPaused, setIsActive, setIsPaused, setSeconds, }]; }; export default useTimer;