overleaf/services/web/frontend/js/shared/hooks/use-debounce.js

18 lines
380 B
JavaScript
Raw Normal View History

import { useEffect, useState } from 'react'
export default function useDebounce(value, delay = 0) {
const [debouncedValue, setDebouncedValue] = useState(value)
useEffect(() => {
const timer = window.setTimeout(() => {
setDebouncedValue(value)
}, delay)
return () => {
window.clearTimeout(timer)
}
}, [value, delay])
return debouncedValue
}