Function: useDebouncedValue()
useDebouncedValue<
T>(value,delay):T
Defined in: use-debounced-value.ts:34
Returns a debounced copy of value that only updates once value has
stopped changing for delay milliseconds.
Every change restarts the wait, so while value keeps changing faster than
delay the returned value stays the same. On the first render it equals
value. It is the debounce counterpart to useThrottledValue, which instead
guarantees regular updates while the value keeps changing. To debounce a
function call rather than a value, use useDebouncedCallback.
Values are compared with Object.is, a changed delay restarts the current
wait, and the pending update is dropped on unmount.
Type Parameters
T
T
The type of the value to debounce.
Parameters
value
T
The value to debounce.
delay
number
How long value must stay unchanged, in milliseconds.
Returns
T
The debounced value.
Example
const [query, setQuery] = useState("");
const debouncedQuery = useDebouncedValue(query, 300);
// Only fetches once the user pauses typing for 300 ms.
const { data } = useApi(`/api/search?q=${encodeURIComponent(debouncedQuery)}`);
return <input value={query} onChange={(e) => setQuery(e.target.value)} />;
For a live, editable example, see the useDebouncedValue docs page.