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

24 lines
470 B
TypeScript
Raw Normal View History

import { useEffect, useState } from 'react'
/**
* @template T
* @param {T} value
* @param {number} delay
* @returns {T}
*/
export default function useDebounce<T>(value: T, delay = 0) {
const [debouncedValue, setDebouncedValue] = useState(value)
useEffect(() => {
const timer = window.setTimeout(() => {
setDebouncedValue(value)
}, delay)
return () => {
window.clearTimeout(timer)
}
}, [value, delay])
return debouncedValue
}