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

24 lines
464 B
JavaScript
Raw Normal View History

import { useEffect, useState } from 'react'
/**
* @template T
* @param {T} value
* @param {number} delay
* @returns {T}
*/
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
}