overleaf/services/web/frontend/js/shared/hooks/use-debounce.js
Jakob Ackermann 618d41f623 Merge pull request #10316 from overleaf/mj-cm6-outline-debouncing
[cm6] Debounce EditorState in CM6 outline

GitOrigin-RevId: 759748f1d3dabbe22dc0cd2d3d989c973753442a
2022-11-08 09:04:25 +00:00

23 lines
464 B
JavaScript

import { useEffect, useState } from 'react'
/**
* @template T
* @param {T} value
* @param {number} delay
* @returns {T}
*/
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
}