Skip to content

Instantly share code, notes, and snippets.

@stern-shawn
Last active March 5, 2020 19:00
Show Gist options
  • Save stern-shawn/18f2aa60e038c4e8c84035675ee6b1d1 to your computer and use it in GitHub Desktop.
Save stern-shawn/18f2aa60e038c4e8c84035675ee6b1d1 to your computer and use it in GitHub Desktop.
useDebounce
import { useState, useEffect } from 'react'
const useDebounce = <T>(value: T, delay: number): T => {
const [debouncedValue, setDebouncedValue] = useState<T>(value)
useEffect(() => {
const handler = setTimeout(() => setDebouncedValue(value), delay)
return () => clearTimeout(handler)
}, [value, delay])
return debouncedValue
}
export default useDebounce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment