useSearch
The useSearch hook consumes the search context provided by the SearchProvider component. You can use this context to change the search state and consume its values.
Import​
import { useSearch } from '@faststore/sdk'
Usage​
// Create a selection sort button
const keyMap = {
price_desc: 'Price, descending',
price_asc: 'Price, ascending',
orders_desc: 'Top sales',
name_asc: 'Name, A-Z',
name_desc: 'Name, Z-A',
release_desc: 'Release date',
discount_desc: 'Discount',
score_desc: 'Relevance',
}
const keys = Object.keys(keyMap)
function Component () {
const {
setSort,
state: { sort },
} = useSearch()
return (
<select
onChange={(e) => setSort(keys[e.target.selectedIndex])}
value={sort}
>
{keys.map((key) => (
<option key={key} value={key}>
{keyMap[key]}
</option>
))}
</select>
)
}
Return​
itemsPerPagenumberDescription
Number of items per page.
stateSearchStateDescription
Current state of the search represented by the selected facets, sort option, and URL path.
setSort(sort: SearchSort) => voidDescription
Sets the sorting criteria of the search results.
setTerm(term: string | null) => voidDescription
Sets the full-text term.
setPage(page: number) => voidDescription
Sets the current page on search pagination.
setFacet(facet: Facet, unique?: boolean) => voidDescription
Selects a specific facet.
removeFacet(facet: Facet) => voidDescription
Removes a specific facet.
toggleFacet(facet: Facet) => voidDescription
Replaces a specific facet.
toggleFacets(facets: Facet[]) => voidDescription
Toggles a specific facet.
addPrevPage() => voidDescription
Prepends a page to the infinite scroll pagination.
addNextPage() => voidDescription
Appends a page to the infinite scroll pagination.