Created
October 24, 2022 16:25
-
-
Save fmartins-andre/5d14ca7915d08519b02873c6b0524d08 to your computer and use it in GitHub Desktop.
Debounce a React.js function
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 { DebouncedFunc } from "lodash" | |
import debounce from "lodash.debounce" | |
import { useMemo } from "react" | |
export default function useDebouncedFn<T extends (...args: any[]) => void>(fn: T, wait?: number) { | |
return useMemo(() =>( | |
debounce((...args:any[]) => {fn(...args)}, wait ?? 300) as DebouncedFunc<T> | |
), [fn, wait]) | |
} |
Author
fmartins-andre
commented
Oct 24, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment