mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-24 21:12:38 -04:00
d959dbc236
[web] Shared React hooks JS to TS conversion GitOrigin-RevId: 0ccdebff236c7424b1a73cd7d6646a9d01a20eb1
23 lines
470 B
TypeScript
23 lines
470 B
TypeScript
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
|
|
}
|