examples/react/algolia
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack React Query Algolia Example#
Searches products on Algolia as you type, with a debounced search term and paginated results loaded with an infinite query.
How it works#
src/services/products/queries.tsdefinesproductsQueries.search(...), aninfiniteQueryOptionsfactory keyed by['products', 'search', indexName, query, hitsPerPage]. Whenqueryis empty,queryFnisskipToken, so nothing is fetched, andgetNextPageParamreturns the next page untilnbPagesis reached.src/services/products/api.tscalls Algolia'ssearchSingleIndexwith the page number and hits per page.src/Search.tsxdebounces the input by 300 ms withuseDebouncedValuefrom TanStack Pacer and passes the debounced term toSearchResults.src/SearchResults.tsxpasses the options touseInfiniteQuerywith aselectthat flattenspagesinto one list of hits, plusstaleTime(30 seconds) andgcTime(15 minutes). The "more" button callsfetchNextPagewhilehasNextPageistrue.
To run this example:
npm installnpm run dev