Created
June 12, 2021 07:45
-
-
Save ilxanlar/592c9c0e68bf4f0444109355eb056b4c to your computer and use it in GitHub Desktop.
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 { useEffect, useRef, useState } from 'react' | |
export default function useNow(interval = 1000) { | |
const [date, setDate] = useState(() => (new Date())) | |
const intervalRef = useRef() | |
useEffect(() => { | |
intervalRef.current = setInterval(() => { | |
setDate(new Date()) | |
}, interval) | |
return () => { | |
clearInterval(intervalRef.current) | |
} | |
}, [interval]) | |
return date | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment