Download react-query/examples/react/algolia/src/useAlgolia.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 821 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/examples/react/algolia/src/useAlgolia.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/examples/react/algolia/src/useAlgolia.ts
-
curl -L -o useAlgolia.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/examples/react/algolia/src/useAlgolia.ts
821 Bytes
| import { skipToken, useInfiniteQuery } from '@tanstack/react-query' | |
| import { search } from './algolia' | |
| export type UseAlgoliaOptions = { | |
| indexName: string | |
| query: string | |
| hitsPerPage?: number | |
| staleTime?: number | |
| gcTime?: number | |
| } | |
| export default function useAlgolia<TData>({ | |
| indexName, | |
| query, | |
| hitsPerPage = 10, | |
| staleTime, | |
| gcTime, | |
| }: UseAlgoliaOptions) { | |
| const queryInfo = useInfiniteQuery({ | |
| queryKey: ['algolia', indexName, query, hitsPerPage], | |
| queryFn: query | |
| ? ({ pageParam }) => | |
| search<TData>({ indexName, query, pageParam, hitsPerPage }) | |
| : skipToken, | |
| initialPageParam: 0, | |
| getNextPageParam: (lastPage) => lastPage.nextPage, | |
| staleTime, | |
| gcTime, | |
| }) | |
| const hits = queryInfo.data?.pages.map((page) => page.hits).flat() | |
| return { ...queryInfo, hits } | |
| } | |