use-debounced-value
Debounces value changes
Source
Docs
Package
Usage
The use-debounced-value hook debounces value changes.
This can be useful when you want to perform a heavy operation based on React state,
for example, sending a search request. Unlike use-debounced-state, it
is designed to work with controlled components.
Value: [empty string]
Debounced value: [empty string]
Differences from use-debounced-state
- You have direct access to the non-debounced value.
- It is used for controlled inputs (
valueprop instead ofdefaultValue), for example, it renders on every state change like a character typed in an input. - It works with props or other state providers, and it does not force use of
useState.
Leading update
You can immediately update the value with the first call using { leading: true } options:
Value: [empty string]
Debounced value: [empty string]
Cancel and flush
The hook returns a third element with cancel and flush handlers.
cancel discards the pending update, flush applies it immediately.
Updates cancel automatically on component unmount.
In this example, type in some text and click the cancel button within a second to cancel debounced value change:
Value: [empty string]
Debounced value: [empty string]
The second element of the returned tuple is a shorthand for cancel for backwards compatibility.
Definition
Exported types
The UseDebouncedValueOptions and UseDebouncedValueReturnValue types are exported from the @mantine/hooks package;
you can import them in your application: