mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-31 21:21:03 -04:00
18 lines
380 B
JavaScript
18 lines
380 B
JavaScript
|
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
|
||
|
}
|