Skip to content

useBatchedCallback

Hook that batches calls and forwards them to a callback

utilities
medium
test coverage
Last changed: 5 days ago

Installation

Library
CLI
Manual
typescript
import { useBatchedCallback } from '@siberiacancode/reactuse';

Usage

typescript
const delayed = useBatchedCallback((batch) => console.log(batch), { size: 5, delay: 1000 });

Demo

Api

Parameters

NameTypeDefaultNote
callback(batch: Params[]) => void-The callback that receives a batch of calls
options.sizenumber-The batch settings with size and optional delay
options.delay?number1000The delay (ms) after which pending calls are flushed

Returns

BatchedCallback<Params>

Type declaration

typescript
export type BatchedCallback<Params extends unknown[]> = ((...args: Params) => void) & {
  flush: () => void;
  cancel: () => void;
};

export interface UseBatchedCallbackOptions {
  delay?: number;
  size: number;
}

Source

SourceDemo

Contributors

D
debabin
debabin

Released under the MIT License.