function useDebounce(cb, delay, deps) {
const initRef = useRef(true)
const [isPending, setIsPending] = useState(false)
useEffect(() => {
if (initRef.current) {
initRef.current = false
return
}
setIsPending(true)
const timeout = setTimeout(() => {
cb()
setIsPending(false)
}, delay)
return () => clearTimeout(timeout)
}, deps)
return isPending
}
name | type | description |
---|---|---|
cb | function | callback function to fire depends on setTimeout |
delay | number | ms (unit), default as 250ms |
deps | array | same as useEffect deps |
name | type | description |
---|---|---|
isPending | boolean |
idle