Skip to content

Instantly share code, notes, and snippets.

@ilxanlar
Created June 12, 2021 07:45
Show Gist options
  • Save ilxanlar/592c9c0e68bf4f0444109355eb056b4c to your computer and use it in GitHub Desktop.
Save ilxanlar/592c9c0e68bf4f0444109355eb056b4c to your computer and use it in GitHub Desktop.
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