mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
618d41f623
[cm6] Debounce EditorState in CM6 outline GitOrigin-RevId: 759748f1d3dabbe22dc0cd2d3d989c973753442a
23 lines
464 B
JavaScript
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
|
|
}
|