Last active
January 9, 2024 11:37
-
-
Save ahsanzizan/2f1a8fc53ed627ced7c82d092e6c182d to your computer and use it in GitHub Desktop.
A nifty hook for keeping track of time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from 'react'; | |
const useTimer = () => { | |
const [seconds, setSeconds] = useState(0); | |
useEffect(() => { | |
const intervalId = setInterval(() => { | |
setSeconds((prevSeconds) => prevSeconds + 1); | |
}, 1000); | |
return () => clearInterval(intervalId); | |
}, []); | |
return seconds; | |
}; | |
export default useTimer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment