Download react-query/examples/react/algolia/src/SearchResults.tsx from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.79 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/examples/react/algolia/src/SearchResults.tsx
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/examples/react/algolia/src/SearchResults.tsx
-
curl -L -o SearchResults.tsx https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/examples/react/algolia/src/SearchResults.tsx
1.79 kB
| import useAlgolia from './useAlgolia' | |
| type Product = { | |
| name: string | |
| shortDescription: string | |
| salePrice: number | |
| } | |
| type SearchResultsProps = { | |
| query: string | |
| } | |
| export default function SearchResults({ query = '' }: SearchResultsProps) { | |
| const { | |
| hits, | |
| isLoading, | |
| isFetching, | |
| status, | |
| hasNextPage, | |
| isFetchingNextPage, | |
| fetchNextPage, | |
| } = useAlgolia<Product>({ | |
| indexName: 'bestbuy', | |
| query, | |
| hitsPerPage: 5, | |
| staleTime: 1000 * 30, // 30s | |
| gcTime: 1000 * 60 * 15, // 15m | |
| }) | |
| if (!query) return null | |
| if (isLoading) return <div className="loading">Loading...</div> | |
| return ( | |
| <div> | |
| <div className="search-status"> | |
| Status: {status} {isFetching && <span>fetching...</span>} | |
| </div> | |
| <div> | |
| <div className="search-result"> | |
| {hits && hits.length > 0 ? ( | |
| hits.map((product) => ( | |
| <li key={product.objectID} className="product"> | |
| <span className="product-name">{product.name}</span> | |
| {product.shortDescription && ( | |
| <> | |
| <br /> | |
| <span className="product-description"> | |
| {product.shortDescription} | |
| </span> | |
| </> | |
| )} | |
| <br /> | |
| <span className="product-price">${product.salePrice}</span> | |
| </li> | |
| )) | |
| ) : ( | |
| <h3>No products found!</h3> | |
| )} | |
| </div> | |
| {hasNextPage && ( | |
| <div className="search-more" onClick={() => fetchNextPage()}> | |
| more | |
| </div> | |
| )} | |
| {isFetchingNextPage && ( | |
| <div className="search-status">Fetching next page...</div> | |
| )} | |
| </div> | |
| </div> | |
| ) | |
| } | |