Skip to content

Instantly share code, notes, and snippets.

@Ebrahim-Ramadan
Created August 11, 2024 21:17
Show Gist options
  • Save Ebrahim-Ramadan/76d96ed8eeb481c016e614e931a19e5f to your computer and use it in GitHub Desktop.
Save Ebrahim-Ramadan/76d96ed8eeb481c016e614e931a19e5f to your computer and use it in GitHub Desktop.
simple use debounce hook in react
import * as React from "react"
export function useDebounce(
value
delay
callback
) {
const [debouncedValue, setDebouncedValue] = React.useState(value)
React.useEffect(() => {
const timer = setTimeout(() => {
setDebouncedValue(value)
callback?.(value)
}, delay ?? 500)
return () => {
clearTimeout(timer)
}
}, [value, delay, callback])
return debouncedValue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment