Skip to main content

useSearchQuerySuggestions

The useSearchQuerySuggestions hook is a specialized wrapper around useFetch used to fetch autocompletion suggestions for search queries. It is typically used in a search input component to provide real-time feedback as the user types. It works on the client as well as on the server.

import { useSearchQuerySuggestions } from '@archibald/search';

const { data: suggestions, isLoading } = useSearchQuerySuggestions(searchTerm, searchOptions, fetchOptions);

Parameters

NameTypeRequiredDescription
searchTermstring✔️The partial search term to get suggestions for.
searchOptionsSearchSuggestionsRequestOptionsOptions for the suggestions request (e.g., max count).
fetchOptionsFetchMinOptionsStandard options for the useFetch hook.

searchTerm

The string representing the user's current input in the search field.

searchOptions

  • Type: SearchSuggestionsRequestOptions
PropertyTypeDescription
maxnumberThe maximum number of suggestions to return.
fieldsstringDefines the depth of data returned for suggestions.

fetchOptions

Standard data fetching options. See useFetch for more information.

Return value

  • Type: FetchResult<SearchSuggestionResponse>
PropertyTypeDescription
dataSearchSuggestionResponse | nullThe list of query suggestions.
isLoadingbooleanTrue if the request is in progress.
isDonebooleanTrue if the request has finished.

Example

import { useSearchQuerySuggestions } from '@archibald/search';

function SearchInput({ value }) {
const { data, isLoading } = useSearchQuerySuggestions(value);

const suggestions = data?.suggestions || [];

return (
<div>
<input type="text" value={value} />
{value.length > 2 && (
<ul>
{suggestions.map(s => (
<li key={s.value}>{s.value}</li>
))}
</ul>
)}
</div>
);
}