Skip to main content

useDebounceState Hook

The useDebounceState hook returns a state together with a copy of its value that updates only after the value has stopped changing for the given delay. It is useful for search fields that call an API.

Usage​

import { useEffect } from "react";
import { InputField, Loader, useDebounceState } from "react-better-html";

function Search() {
const [query, debouncedQuery, setQuery, isLoading] = useDebounceState("", 0.5);

useEffect(() => {
if (!debouncedQuery) return;

searchUsers(debouncedQuery);
}, [debouncedQuery]);

return (
<>
<InputField.search value={query} onChangeValue={setQuery} />

{isLoading && <Loader.text text="Searching..." />}
</>
);
}

Return Value​

[
value: Value,
debouncedValue: Value,
setValue: React.Dispatch<React.SetStateAction<Value>>,
isLoading: boolean
]
  • value - the current value
  • debouncedValue - the value after the delay has passed without changes
  • setValue - changes the value
  • isLoading - true while waiting for the delay after a change

Notes​