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 valuedebouncedValue- the value after the delay has passed without changessetValue- changes the valueisLoading-truewhile waiting for the delay after a change
Notes
- The delay is in seconds (defaults to
0.5). <InputField>and<Dropdown>have a built-inwithDebounceprop.