Skip to content

Instantly share code, notes, and snippets.

@MauricioRobayo
Last active May 20, 2021 15:39
Show Gist options
  • Save MauricioRobayo/a71353330edcbd201b0c6dc809ce9c13 to your computer and use it in GitHub Desktop.
Save MauricioRobayo/a71353330edcbd201b0c6dc809ce9c13 to your computer and use it in GitHub Desktop.
#gogofast
import { useState, useEffect } from "react";
function 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