wkkkis-hooks

use-debounce-callback (use-debounce-callback)

Debounce the call of a function.

v0.1.0

Usage

import { use-debounce-callback } from "@/hooks"
1"use client"; 2 3import React, { useCallback, useState } from "react"; 4 5import { 6 Card, 7 CardContent, 8 CardDescription, 9 CardFooter, 10 CardHeader, 11 CardTitle, 12} from "@/components/ui/card"; 13import { Input } from "@/components/ui/input"; 14 15import { useDebounceCallback } from "@/components/hooks/use-debounce-callback"; 16 17export function UseDebounceCallbackDemo() { 18 const [inputValue, setInputValue] = useState(""); 19 const [debouncedValue, setDebouncedValue] = useState(""); 20 21 const updateDebouncedValue = useCallback((value: string) => { 22 setDebouncedValue(value); 23 }, []); 24 25 const debouncedUpdate = useDebounceCallback(updateDebouncedValue, 500); 26 27 const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { 28 const value = e.target.value; 29 setInputValue(value); 30 debouncedUpdate(value); 31 }; 32 33 return ( 34 <Card className="max-w-md w-full relative"> 35 <CardHeader> 36 <CardTitle>useDebounceCallback</CardTitle> 37 <CardDescription> 38 This component uses the <code>useDebounceCallback</code> hook to 39 debounce the callback function to control the input value. 40 </CardDescription> 41 </CardHeader> 42 <CardContent> 43 <Input 44 type="text" 45 value={inputValue} 46 onChange={handleChange} 47 placeholder="Type something..." 48 className="w-full" 49 /> 50 </CardContent> 51 <CardFooter className="flex-col items-start gap-2"> 52 <p className="text-sm text-muted-foreground"> 53 <strong>Immediate value:</strong> {inputValue} 54 </p> 55 <p className="text-sm text-muted-foreground"> 56 <strong>Debounced value:</strong> {debouncedValue} 57 </p> 58 </CardFooter> 59 </Card> 60 ); 61} 62

Install

npx wkkkis-hooks add use-debounce-callback

Options

NameTypeDefaultDescription
delaynumber500The delay in milliseconds before the function is executed.
callbackgeneric TundefinedThe function to be executed after the delay.

Returns — Properties

NameTypeDefaultDescription
Thereturned function accepts the same arguments as the original callback.
Ifcalled repeatedly, only the last call within the delay period will trigger the callback.
Thedebounced function is stable (does not change identity unless delay changes).

Callback & Delay Handling

  • The hook always uses the latest version of the callback, even if it changes between renders.
  • The delay can be changed dynamically; the new delay will be used for subsequent calls.

Returned Debounced Function

  • The returned function accepts the same arguments as the original callback.
  • If called repeatedly, only the last call within the delay period will trigger the callback.
  • The debounced function is stable (does not change identity unless delay changes).

Cleanup

  • Any pending timeout is cleared when the component unmounts, preventing memory leaks or unexpected calls.

Best Practices & Caveats

  • The hook is client-side only.
  • For best performance, memoize the callback if it depends on other values.
  • If you need to cancel a pending debounce manually, you must manage that outside the hook.
  • The hook does not guarantee the callback will run if the component unmounts before the delay.

Files in this hook

SourceDestination
files/use-debounce-callback.tssrc/hooks/use-debounce-callback.ts