Skip to content

Instantly share code, notes, and snippets.

@sameroso
Created October 25, 2021 13:02
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 sameroso/085f5f2cdbdf6da226038d02992e3dde to your computer and use it in GitHub Desktop.
Save sameroso/085f5f2cdbdf6da226038d02992e3dde to your computer and use it in GitHub Desktop.
import { useMemo } from "react";
import { debounce } from "lodash";
export function useDebounceCallback(callback: (...values: any[]) => void, delay: number) {
// eslint-disable-next-line react-hooks/exhaustive-deps
const onChangeDebounce = useMemo(() => debounce(callback, delay), []);
useEffect(() => {
return () => onChangeDebounce.cancel();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return onChangeDebounce;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment