Last active
September 9, 2020 21:32
-
-
Save hyber1z0r/03d896ead4cce709a331f2e5b1d89e9d to your computer and use it in GitHub Desktop.
useDebounce implementation
This file contains 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(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