Skip to content

Instantly share code, notes, and snippets.

@csenio
Created October 28, 2019 14:51
Show Gist options
  • Save csenio/3cd991334ef2ee61d6a292ca8222b43a to your computer and use it in GitHub Desktop.
Save csenio/3cd991334ef2ee61d6a292ca8222b43a to your computer and use it in GitHub Desktop.
import {useState, useEffect} from 'react'
export default function useDebounce(value) {
const [debouncedValue, setDebouncedValue] = useState(value)
useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value)
}, 500)
return () => {
clearTimeout(handler)
}
}, [value])
return debouncedValue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment