overleaf/services/web/frontend/js/shared/hooks/use-debounce.js
Alf Eaton d7b997e86c Merge pull request #4038 from overleaf/ae-move-use-debounce
Move useDebounce hook

GitOrigin-RevId: f047e279ca1694fc4fa68aa098d6608edefd20fe
2021-05-15 02:09:58 +00:00

17 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
}