Skip to content

Instantly share code, notes, and snippets.

@numToStr
Created March 12, 2020 07:08
Show Gist options
  • Save numToStr/2bd4768d93d17c964c91656cf6ebfc5e to your computer and use it in GitHub Desktop.
Save numToStr/2bd4768d93d17c964c91656cf6ebfc5e to your computer and use it in GitHub Desktop.
const useDebounce = () => {
let timeout = useRef<NodeJS.Timeout | null>(null);
return useCallback(
(fn: Function, wait: number) => (e: FormEvent) => {
e.preventDefault();
timeout.current && clearTimeout(timeout.current);
timeout.current = setTimeout(() => {
fn();
}, wait);
},
[]
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment