use-debounce-fn
shadcn-hooks/use-debounce-fnFreeHook
A hook to debounce a function
Install it
npx shadcn@latest add https://shadcn-hooks.com/r/use-debounce-fn.jsonSource
1import { debounce } from 'es-toolkit'2import { useMemo } from 'react'3import { useLatest } from '@/registry/hooks/use-latest'4import { useUnmount } from '@/registry/hooks/use-unmount'5import type { DebounceOptions } from 'es-toolkit'67export type { DebounceOptions }89export function useDebounceFn<Fn extends (...args: any[]) => any>(10 fn: Fn,11 debounceMs?: number,12 options?: DebounceOptions,13) {14 const fnRef = useLatest(fn)1516 const debouncedFn = useMemo(17 () =>18 debounce(19 (...args: Parameters<Fn>) => fnRef.current(...args),20 debounceMs ?? 1000,21 options,22 ),23 [],24 )2526 useUnmount(() => debouncedFn.cancel())2728 return {29 run: debouncedFn,30 cancel: debouncedFn.cancel,31 flush: debouncedFn.flush,32 }33}
What it pulls in
npm packages
Other registry items
Files it writes
More from shadcn-hooks
All 58 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| use-active-elementuse-active-element | shadcn-hooks | Hooks | Free | no deps | |
| use-batteryuse-battery | shadcn-hooks | Hooks | Free | no deps | |
| use-booleanuse-boolean | shadcn-hooks | Hooks | Free | no deps | |
| use-click-any-whereuse-click-any-where | shadcn-hooks | Hooks | Free | no deps | |
| use-click-awayuse-click-away | shadcn-hooks | Hooks | Free | no deps | |
| use-clipboarduse-clipboard | shadcn-hooks | Hooks | Free | no deps | |
| use-controllable-valueuse-controllable-value | shadcn-hooks | Hooks | Free | 1 dep | |
| use-counteruse-counter | shadcn-hooks | Hooks | Free | no deps |
