Skip to content

Instantly share code, notes, and snippets.

@washingtonsoares
Created July 11, 2023 09:35
Show Gist options
  • Save washingtonsoares/481e5b31c0a1d3a2d0b92fe7736e7ebb to your computer and use it in GitHub Desktop.
Save washingtonsoares/481e5b31c0a1d3a2d0b92fe7736e7ebb to your computer and use it in GitHub Desktop.
import * as React from "react"
export function useDebounce<T>(value: T, delay?: number): T {
const [debouncedValue, setDebouncedValue] = React.useState<T>(value)
React.useEffect(() => {
const timer = setTimeout(() => setDebouncedValue(value), delay || 500)
return () => {
clearTimeout(timer)
}
}, [value, delay])
return debouncedValue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment