useThrottledCallback

A performance-optimization hook that limits the execution frequency of a callback to one per specified time window.

Important

Throttling is ideal for high-frequency events like scroll, resize, or mousemove. It guarantees the callback runs at a consistent rate (once every delay ms).

Caution

Unlike debounce, throttle will execute the first call immediately but will discard subsequent calls until the delay has passed.

Installation

bash

Description

The useThrottledCallback transforms a high-frequency function into a throttled version that executes at most once every delay milliseconds. It is specifically designed for handling intensive events like scroll, resize, or mousemove where executing logic on every browser trigger would degrade UI performance.

The hook utilizes a Leading-Edge execution strategy: the first invocation triggers the callback inmediately, while subsequent calls within the delay window are ignored.

Parameters

NameTypeDescription
callback(...args: TArgs) => voidThe function to be throttled.
options.delaynumberMinimum time in milliseconds between executions.

Return values

NameTypeDescription
throttledCallback(...args: TArgs) => voidThe rate-limited version of the provided callback.

Demo

Throttled Callback

Move mouse here

Check the counters below
Raw events

0

Updating ~60-120fps
Throttled (100ms)

0

Max 10 updates per second

Source code

tsx