useSearch
The useSearch function is a React hook used to show search result, perform search and filter the results.
Usage
The useSearch hook can only be used in search page types.
import { useSearch } from "builderoo";
const { results, filters, query, setQuery, toggleFilter, searchAgain, loading } = useSearch()Return Values
| Name | Type | Description |
|---|---|---|
| results | SearchResult | The search results. |
| filters | SearchFilter[] | Filters to drill down the results. |
| query | string | Current search query. Used to show query in the search box or elsewhere. |
| setQuery | function | Used to set the search query as the user types. |
| toggleFilter | function | A function toggle a search filter selection. |
| searchAgain | function | Perform the search again with new query and selected filters. |
| loading | boolean | If the search is currently loading |
Notes:
- Here’s the signature of the function
toggleFilter
function toggleFilter(name: string, type: string | undefined, choice: string, apply: boolean = true)Calling toggleFilter automatically performs the search again. To apply multiple filters,
then apply once, call toggleFilter with apply = false value.
- Instead of using another state variable,
queryandsetQuerycan be used in the search input value and to set value.setQuerydoes not automatically perform the search again. CallsearchAgainto perform the search again with updated query and selected filters.