mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -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
|
||
|
}
|