mirror of
https://github.com/seigler/triplebyte-react-spa
synced 2025-07-26 23:26:10 +00:00
Initial commit from Create Next App
This commit is contained in:
commit
5684ef36ec
10 changed files with 7925 additions and 0 deletions
19
lib/useInterval.js
Normal file
19
lib/useInterval.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
import { useEffect, useRef } from 'react'
|
||||
|
||||
// https://overreacted.io/making-setinterval-declarative-with-react-hooks/
|
||||
const useInterval = (callback, delay) => {
|
||||
const savedCallback = useRef()
|
||||
useEffect(() => {
|
||||
savedCallback.current = callback
|
||||
}, [callback])
|
||||
useEffect(() => {
|
||||
const handler = (...args) => savedCallback.current(...args)
|
||||
|
||||
if (delay !== null) {
|
||||
const id = setInterval(handler, delay)
|
||||
return () => clearInterval(id)
|
||||
}
|
||||
}, [delay])
|
||||
}
|
||||
|
||||
export default useInterval
|
Loading…
Add table
Add a link
Reference in a new issue