Skip to content

Instantly share code, notes, and snippets.

@ahsanzizan
Last active January 9, 2024 11:37
Show Gist options
  • Save ahsanzizan/2f1a8fc53ed627ced7c82d092e6c182d to your computer and use it in GitHub Desktop.
Save ahsanzizan/2f1a8fc53ed627ced7c82d092e6c182d to your computer and use it in GitHub Desktop.
A nifty hook for keeping track of time
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