Last active
March 5, 2020 19:00
-
-
Save stern-shawn/18f2aa60e038c4e8c84035675ee6b1d1 to your computer and use it in GitHub Desktop.
useDebounce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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