mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-12 16:23:36 -05: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
|
||
|
}
|