useDebounceValue
zyeon/use-debounce-valueFreeHook
A generic hook that returns a value only after it has stopped changing for a delay, with no flash on first render.
See it running
Open the demo on zyeon
ui.zyeon.ai/r/use-debounce-valueInstall it
npx shadcn@latest add https://ui.zyeon.ai/r/use-debounce-value.jsonSource
1"use client"23import * as React from "react"45/**6 * Debounces a fast-changing value. The first render returns `value` as-is7 * (no flash-to-empty), then `debouncedValue` only catches up to the latest8 * `value` once `delay` ms have passed without `value` changing again — every9 * new `value` restarts the wait. Changing `delay` also restarts the timer10 * with the new duration. The pending timer is cleared on unmount (and before11 * every re-run), so no stray `setState` fires after the consumer is gone.12 */13export function useDebounceValue<T>(value: T, delay = 300): T {14 const [debouncedValue, setDebouncedValue] = React.useState(value)1516 React.useEffect(() => {17 const timeout = setTimeout(() => {18 setDebouncedValue(value)19 }, delay)2021 return () => clearTimeout(timeout)22 }, [value, delay])2324 return debouncedValue25}2627export default useDebounceValue
Files it writes
More from zyeon
All 517 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| useAsyncuse-async | zyeon | Hooks | Free | no deps | |
| useBroadcastChanneluse-broadcast-channel | zyeon | Hooks | Free | no deps | |
| useClickOutsideuse-click-outside | zyeon | Hooks | Free | no deps | |
| useClipboardPasteuse-clipboard-paste | zyeon | Hooks | Free | no deps | |
| useControllableStateuse-controllable-state | zyeon | Hooks | Free | no deps | |
| useCopyToClipboarduse-copy-to-clipboard | zyeon | Hooks | Free | no deps | |
| useCountdownuse-countdown | zyeon | Hooks | Free | no deps | |
| useDocumentTitleuse-document-title | zyeon | Hooks | Free | no deps |
