UNPKG

debouncesp

Version:

A minimal debounce utility and React hook to prevent spamming button clicks or function calls.

15 lines (11 loc) 339 B
import { useRef } from "react"; export default function useDebounce(callback, delay) { const timeoutRef = useRef(null); const debouncedCallback = (...args) => { clearTimeout(timeoutRef.current); timeoutRef.current = setTimeout(() => { callback(...args); }, delay); }; return debouncedCallback; }