Skip to main content

useDebouncedValue

function useDebouncedValue<T>(value: T, delayMs: number): T;

Returns value, but only updates once value has stopped changing for delayMs. The first render returns value straight away, and changing delayMs restarts the wait. A delay of 0 or less (or NaN) follows value after the current task, and Infinity keeps the first value. Use it to derive expensive work (a fetch, a filter over a big list) from fast-changing input.

Live demo​

Type to search

Example​

import { useState } from "react";
import { useDebouncedValue } from "react-honey-hooks";

function ProductSearch() {
const [query, setQuery] = useState("");
const debouncedQuery = useDebouncedValue(query, 300);
const results = useProducts(debouncedQuery); // only refetches after typing pauses

return (
<>
<input
value={query}
onChange={(event) => {
setQuery(event.target.value);
}}
/>
<ProductList products={results} />
</>
);
}