useSuspenseInfiniteQuery(...)
The Hook enables you to perform asynchronous data fetching operations with support for infinite scrolling scenarios. The functionality is similar to the useInfiniteQuery hook, but with the added benefit of Suspense support. For more detailed information, explore useSuspenseInfiniteQuery(...) ๐ด documentation for more details.
const query = qraft.<service>.<operation>.useSuspenseInfiniteQuery(
parameters,
suspenseInfiniteQueryOptions,
queryClient,
)
Argumentsโ
-
parameters: { path, query, header } | QueryKey | {}- Required and strictly-typed โจ parameters for the query
parameterswill be used to generate theQueryKey- Instead of an object with
{path, query, header}, you can pass aQueryKeyas an array which is also strictly-typed โจ - If operation does not require parameters, you must pass an empty object
{}for strictness
-
suspenseInfiniteQueryOptions?: UseSuspenseInfiniteQueryOptions- Optional, represents the options of the
useSuspenseInfiniteQuery(...) ๐ด
Hook
queryOptions.queryFncould be provided to override the defaultqueryFnused by Qraftis not allowed, as it will be generated fromqueryOptions.queryKeyparameters
- Optional, represents the options of the
useSuspenseInfiniteQuery(...) ๐ด
Hook
-
queryClient?: QueryClient- Optional QueryClient ๐ด to be used
- If not provided
QraftContext.queryClientwill be used if available- useQueryClient() ๐ด result will be used as a fallback
Returnsโ
Same object as UseInfiniteQueryResult object, except that:
datais guaranteed to be definedisPlaceholderDatais missingstatusis always success- the derived flags are set accordingly.
See the TanStack useSuspenseInfiniteQuery(...) ๐ด documentation for more details.
Examplesโ
src/PostList.tsx
import { Suspense } from 'react';
import { createAPIClient } from './api'; // generated by OpenAPI Qraft CLI
const qraft = createAPIClient();
function PostList() {
/**
* `qraft.posts.getPosts.useSuspenseInfiniteQuery(...)` launches the initial fetch operation:
* ###
* GET /posts?limit=10&page=1
*/
const infiniteQuery = qraft.posts.getPosts.useSuspenseInfiniteQuery(
{ query: { limit: 10 } },
{
getNextPageParam: (lastPage, allPages, lastPageParams) => {
if (lastPage.length < 10) return; // if less than 10 items, there are no more pages
return {
query: {
page: Number(lastPageParams.query?.page) + 1,
},
};
},
initialPageParam: {
query: {
page: 1, // will be used in initial request
},
},
}
);
return (
<div>
{infiniteQuery.data.pages.map((page, pageIndex) => (
<ul key={pageIndex}>
{page.map((post) => (
<li key={post.id}>{post.title}</li>
))}
</ul>
))}
<button onClick={() => {
// โฌ๏ธ Initiates a GET request: GET /posts?limit=10&page=2
infiniteQuery.fetchNextPage()
}}>
Load More
</button>
</div>
);
}
export default function() {
return (
<Suspense fallback={<div>Loading...</div>}>
<PostList />
</Suspense>
);
}