useDebounceCallback
Custom hook that creates a debounced version of a callback function.
Installation
npx sse-tool add use-debounce-callback
npm install sse-hooks
Usage
import { useDebounceCallback } from "./{hooks file}";
const debouncedCallback = useDebounceCallback((searchTerm) => {
// Perform search after user stops typing for 500 milliseconds
searchApi(searchTerm);
}, 500);
// Later in the component
debouncedCallback("react hooks"); // Will invoke the callback after 500 milliseconds of inactivity.
import { useDebounceCallback } from "sse-hooks";
const debouncedCallback = useDebounceCallback((searchTerm) => {
// Perform search after user stops typing for 500 milliseconds
searchApi(searchTerm);
}, 500);
// Later in the component
debouncedCallback("react hooks"); // Will invoke the callback after 500 milliseconds of inactivity.
API
Parameters
| Name | Type | Description |
|---|---|---|
| func | T | The callback function to be debounced. |
| delay | number | The delay in milliseconds before the callback is invoked (default is 500 milliseconds). |
| options | DebounceOptions | Options to control the behavior of the debounced function. |
Return Value
Returns DebouncedState.
A debounced version of the original callback along with control functions.