Skip to content

Instantly share code, notes, and snippets.

@everdimension
Created August 5, 2022 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save everdimension/3850655e7a79500bfc9b464007865cb8 to your computer and use it in GitHub Desktop.
Save everdimension/3850655e7a79500bfc9b464007865cb8 to your computer and use it in GitHub Desktop.
import { useEffect, useMemo } from 'react';
import debounce from 'lodash/debounce';
export function useDebouncedCallback(
callback: (...args: any) => any,
delay: number,
) {
const debouncedCallback = useMemo(
() => debounce(callback, delay),
[callback, delay],
);
useEffect(() => {
return debouncedCallback.cancel;
}, [debouncedCallback.cancel]);
return debouncedCallback;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment