Created
August 5, 2022 20:10
-
-
Save everdimension/3850655e7a79500bfc9b464007865cb8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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