Created
January 21, 2024 18:29
-
-
Save rossnelson/65471e4b7e962abac542cfb47264cd4c to your computer and use it in GitHub Desktop.
Debounce in react
This file contains 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 { useCallback } from 'react'; | |
import debounce from 'lodash.debounce'; | |
export function useDebounce(fn: () => void, delay: number) { | |
const debouncer = debounce(fn, delay); | |
const start = useCallback(() => { | |
debouncer(); | |
}, [debouncer]); | |
const stop = useCallback(() => { | |
debouncer.cancel(); | |
}, [debouncer]); | |
return { start, stop }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment