Skip to content

Instantly share code, notes, and snippets.

@a2ankitrai
Forked from ryanto/use-debounce.js
Created October 26, 2024 03:14
Show Gist options
  • Select an option

  • Save a2ankitrai/a9404228da6f25bc5d7a678148683af3 to your computer and use it in GitHub Desktop.

Select an option

Save a2ankitrai/a9404228da6f25bc5d7a678148683af3 to your computer and use it in GitHub Desktop.
function useDebounced(value, timeout) {
let [debouncedValue, setDebouncedValue] = useState(value)
useEffect(() => {
let timeoutId = setTimeout(() => {
setDebouncedValue(value)
}, timeout)
return () => {
clearTimeout(timeoutId)
}
}, [value, timeout])
return debouncedValue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment