Skip to content

Instantly share code, notes, and snippets.

@rossnelson
Created January 21, 2024 18:29
Show Gist options
  • Save rossnelson/65471e4b7e962abac542cfb47264cd4c to your computer and use it in GitHub Desktop.
Save rossnelson/65471e4b7e962abac542cfb47264cd4c to your computer and use it in GitHub Desktop.
Debounce in react
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