useDebouncedState

A state management hook that tracks immediate value changes while providing a delayed update for performance-heavy operations.

Note

Perfect for search inputs or filter bars. It ensures that expensive operations (like API calls) only fire after the user has stopped typing for the duration of the delay.

Tip

The value property provides the immediate state for a responsive UI, while debouncedValue should be used as a dependency for your useEffect or data fetching.

Installation

bash

Description

The useDebouncedState manages two versions of a state: an immediate value for responsive UI (like input typing) and a debounced value for expensive side effects (like API calls or heavy filtering). The debounced value only updates once the user has stopped changing the immediate value for a specified duration.

This pattern is the industry standard for search bars, preventing "server hammering" by ensuring a request is only sent after the user finishes typing their query.

Parameters

NameTypeDescription
initialValueTThe starting value for both state versions.
options.delaynumberInactivity period in milliseconds before updating the debounced value.

Return values

NameTypeDescription
valueTThe current, inmediate state (use for controlled inputs).
debouncedValueTThe delayed state (use for effects or API calls).
setValueDispatch<SetStateAction<T>>Standard React state setter for the inmediate value.

Demo

Debounced State

Immediate
Debounced
The Debounced state only catches up 500ms after you stop typing.

Source code

tsx