UseDebounceCallback
guarahooks/use-debounce-callbackFreeHook
Debounce the call of a function.
Live preview
live · rendered by the registry
Rendered by guarahooks on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://guarahooks.com/r/use-debounce-callback.jsonSource
1'use client';23import { useCallback, useEffect, useRef } from 'react';45export function useDebounceCallback<6 Fn extends (...args: Parameters<Fn>) => void,7>(callback: Fn, delay: number = 500): (...args: Parameters<Fn>) => void {8 const callbackRef = useRef<typeof callback>(callback);9 const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);1011 useEffect(() => {12 callbackRef.current = callback;13 }, [callback]);1415 const debouncedFn = useCallback(16 (...args: Parameters<Fn>) => {17 if (timeoutRef.current) {18 clearTimeout(timeoutRef.current);19 }20 timeoutRef.current = setTimeout(() => {21 callbackRef.current(...args);22 }, delay);23 },24 [delay],25 );2627 // Clear the timeout when the component unmounts28 useEffect(() => {29 return () => {30 if (timeoutRef.current) {31 clearTimeout(timeoutRef.current);32 }33 };34 }, []);3536 return debouncedFn;37}
Files it writes
More from guarahooks
All 100 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| UseAbortControlleruse-abort-controller | guarahooks | Hooks | Free | no deps | |
| UseArrayStateuse-array-state | guarahooks | Hooks | Free | no deps | |
| UseAxiosuse-axios | guarahooks | Hooks | Free | no deps | |
| UseBatteryStatususe-battery-status | guarahooks | Hooks | Free | no deps | |
| UseBetterAuthuse-better-auth | guarahooks | Hooks | Free | no deps | |
| UseClickOutsideuse-click-outside | guarahooks | Hooks | Free | no deps | |
| UseConfirmuse-confirm | guarahooks | Hooks | Free | no deps | |
| UseCookieuse-cookie | guarahooks | Hooks | Free | no deps |
