useDebounceCallback

Custom hook that creates a debounced version of a callback function.

Custom hook that creates a debounced version of a callback function.

Usage

import { useDebounceCallback } from "@tilt-legal/cubitt-components/utilities/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

Signature

declare function useDebounceCallback<T extends (...args: any) => ReturnType<T>>(func: T, delay?: number, options?: DebounceOptions): DebouncedState<T>;

Parameters

ParameterTypeDescriptionDefault
funcTThe callback function to be debounced.-
delaynumberThe delay in milliseconds before the callback is invoked ( milliseconds).500
[options]DebounceOptionsOptions to control the behavior of the debounced function.-

Return Value

ValueTypeDescription
returnDebouncedState&lt;T&gt;A debounced version of the original callback along with control functions.

On this page