use-throttle-fn
shadcn-hooks/use-throttle-fnFreeHook
A hook to throttle a function
Install it
npx shadcn@latest add https://shadcn-hooks.com/r/use-throttle-fn.jsonSource
1import { throttle } from 'es-toolkit'2import { useMemo } from 'react'3import { useLatest } from '@/registry/hooks/use-latest'4import { useUnmount } from '@/registry/hooks/use-unmount'5import type { ThrottleOptions } from 'es-toolkit'67export type { ThrottleOptions }89export function useThrottleFn<Fn extends (...args: any[]) => any>(10 fn: Fn,11 throttleMs?: number,12 options?: ThrottleOptions,13) {14 const fnRef = useLatest(fn)1516 const throttledFn = useMemo(17 () =>18 throttle(19 (...args: Parameters<Fn>) => fnRef.current(...args),20 throttleMs ?? 1000,21 options,22 ),23 [],24 )2526 useUnmount(() => throttledFn.cancel())2728 return {29 run: throttledFn,30 cancel: throttledFn.cancel,31 flush: throttledFn.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 |
