CoastalCommitsPastes/client/lib/hooks/use-debounce.ts

19 lines
379 B
TypeScript
Raw Permalink Normal View History

2022-03-23 19:28:39 -04:00
// useDebounce.js
import { useState, useEffect } from "react"
2022-03-23 19:28:39 -04:00
export default function useDebounce(value: any, delay: number) {
const [debouncedValue, setDebouncedValue] = useState(value)
2022-03-23 19:28:39 -04:00
useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value)
}, delay)
2022-03-23 19:28:39 -04:00
return () => {
clearTimeout(handler)
}
}, [value, delay])
2022-03-23 19:28:39 -04:00
return debouncedValue
2022-03-23 19:28:39 -04:00
}