Skip to content

Instantly share code, notes, and snippets.

@hyber1z0r
Last active September 9, 2020 21:32
Show Gist options
  • Save hyber1z0r/03d896ead4cce709a331f2e5b1d89e9d to your computer and use it in GitHub Desktop.
Save hyber1z0r/03d896ead4cce709a331f2e5b1d89e9d to your computer and use it in GitHub Desktop.
useDebounce implementation
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