Skip to content

Instantly share code, notes, and snippets.

@Krisztiaan
Created November 17, 2020 11:20
Show Gist options
  • Save Krisztiaan/959c5d3e2c13950af7476609fe753d97 to your computer and use it in GitHub Desktop.
Save Krisztiaan/959c5d3e2c13950af7476609fe753d97 to your computer and use it in GitHub Desktop.
import { useState, useEffect } from 'react'
export default function useDebouncedState<T>(value: T, delay: number) {
const [debouncedValue, setDebouncedValue] = useState<T>(value)
useEffect(() => {
const handler = setTimeout(() => setDebouncedValue(value), delay)
return () => clearTimeout(handler)
}, [value, delay, setDebouncedValue])
return debouncedValue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment