2022-03-23 21:21:46 -04:00
|
|
|
import { useState, useEffect } from "react"
|
2022-03-23 19:28:39 -04:00
|
|
|
|
2022-04-12 02:07:06 -04:00
|
|
|
export default function useDebounce<T>(value: T, delay: number) {
|
2022-03-23 21:21:46 -04:00
|
|
|
const [debouncedValue, setDebouncedValue] = useState(value)
|
2022-03-23 19:28:39 -04:00
|
|
|
|
2022-03-23 21:21:46 -04:00
|
|
|
useEffect(() => {
|
|
|
|
const handler = setTimeout(() => {
|
|
|
|
setDebouncedValue(value)
|
|
|
|
}, delay)
|
2022-03-23 19:28:39 -04:00
|
|
|
|
2022-03-23 21:21:46 -04:00
|
|
|
return () => {
|
|
|
|
clearTimeout(handler)
|
|
|
|
}
|
|
|
|
}, [value, delay])
|
2022-03-23 19:28:39 -04:00
|
|
|
|
2022-03-23 21:21:46 -04:00
|
|
|
return debouncedValue
|
2022-03-23 19:28:39 -04:00
|
|
|
}
|