MCPcopy Create free account
hub / github.com/TanStack/query / SearchResults

Function SearchResults

examples/react/algolia/src/SearchResults.tsx:13–72  ·  view source on GitHub ↗
({ query = '' }: SearchResultsProps)

Source from the content-addressed store, hash-verified

11}
12
13export default function SearchResults({ query = '' }: SearchResultsProps) {
14 const {
15 hits,
16 isLoading,
17 isFetching,
18 status,
19 hasNextPage,
20 isFetchingNextPage,
21 fetchNextPage,
22 } = useAlgolia<Product>({
23 indexName: 'bestbuy',
24 query,
25 hitsPerPage: 5,
26 staleTime: 1000 * 30, // 30s
27 gcTime: 1000 * 60 * 15, // 15m
28 })
29
30 if (!query) return null
31
32 if (isLoading) return <div className="loading">Loading...</div>
33
34 return (
35 <div>
36 <div className="search-status">
37 Status: {status} {isFetching && <span>fetching...</span>}
38 </div>
39 <div>
40 <div className="search-result">
41 {hits && hits.length > 0 ? (
42 hits.map((product) => (
43 <li key={product.objectID} className="product">
44 <span className="product-name">{product.name}</span>
45 {product.shortDescription && (
46 <>
47 <br />
48 <span className="product-description">
49 {product.shortDescription}
50 </span>
51 </>
52 )}
53 <br />
54 <span className="product-price">${product.salePrice}</span>
55 </li>
56 ))
57 ) : (
58 <h3>No products found!</h3>
59 )}
60 </div>
61 {hasNextPage && (
62 <div className="search-more" onClick={() => fetchNextPage()}>
63 more
64 </div>
65 )}
66 {isFetchingNextPage && (
67 <div className="search-status">Fetching next page...</div>
68 )}
69 </div>
70 </div>

Callers

nothing calls this directly

Calls 1

useAlgoliaFunction · 0.70

Tested by

no test coverage detected