Open History to see the last commit
1---
2id: usePrefetchInfiniteQuery
3title: usePrefetchInfiniteQuery
4redirect_from:
5 - framework/vue/reference/usePrefetchInfiniteQuery
6---
7β
8```ts
9function usePrefetchInfiniteQuery<TQueryFnData, TError, TData, TQueryKey, TPageParam>(options: MaybeRefOrGetter<MaybeRefDeep<UsePrefetchInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>>>, queryClient?: QueryClient): void;
10```
11β
12Defined in: [packages/vue-query/src/usePrefetchInfiniteQuery.ts:100](https://github.com/TanStack/query/blob/main/packages/vue-query/src/usePrefetchInfiniteQuery.ts#L100)
13β
14`usePrefetchInfiniteQuery` does not return anything β it fires a prefetch as a reactive side effect, useful
15for kicking off a fetch ahead of the component that will actually render the data with `useInfiniteQuery`.
16You can pass everything to `usePrefetchInfiniteQuery` that you can pass to `queryClient.infiniteQuery`,
17though `queryKey`, `initialPageParam`, and `getNextPageParam` are always required, and `queryFn` is required
18unless a default query function has been defined.
19β
20`getNextPageParam` receives both the last page of the infinite list of data and the full array of all pages,
21as well as pageParam information, and should return a single variable that will be passed to your query
22function as `context.pageParam`. Return `undefined` or `null` to indicate there is no next page available.
23β
24The prefetch is skipped if the query already has any cached state β including a `pending`/`error` state left
25over from a previous attempt β so it won't refetch data that's already there or already in flight. It
26re-runs whenever a reactive dependency in `options` (built with `infiniteQueryOptions`, for example) changes.
27β
28Fire this during render, before a suspense boundary that wraps a component using `useInfiniteQuery`'s
29`suspense()` β see the [Suspense guide](https://tanstack.com/query/latest/docs/framework/vue/guides/suspense).
30β
31## Type Parameters
32β
33### TQueryFnData
34β
35`TQueryFnData` = `unknown`
36β
37### TError
38β
39`TError` = `Error`
40β
41### TData
42β
43`TData` = [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `unknown`\>
44β
45### TQueryKey
46β
47`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
48β
49### TPageParam
50β
51`TPageParam` = `unknown`
52β
53## Parameters
54β
55### options
56β
57`MaybeRefOrGetter`\<`MaybeRefDeep`\<[`UsePrefetchInfiniteQueryOptions`](../type-aliases/UsePrefetchInfiniteQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>\>\>
58β
59A `ref`, plain value, or reactive getter resolving to the
60[UsePrefetchInfiniteQueryOptions](../type-aliases/UsePrefetchInfiniteQueryOptions.md) to use β everything you can pass to `queryClient.infiniteQuery`.
61β
62### queryClient?
63β
64[`QueryClient`](../classes/QueryClient.md)
65β
66Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`
67will be used.
68β
69## Returns
70β
71`void`
72β
73## Example
74β
75```vue
76<script setup lang="ts">
77import { infiniteQueryOptions, usePrefetchInfiniteQuery } from '@tanstack/vue-query'
78import Projects from './Projects.vue'
79β
80const projectsOptions = infiniteQueryOptions({
81 queryKey: ['projects'],
82 queryFn: ({ pageParam }) => fetchProjects(pageParam),
83 initialPageParam: 0,
84 getNextPageParam: (lastPage) => lastPage.nextId,
85})
86β
87// Fire the prefetch as soon as this component runs, before `Projects` mounts and calls `useInfiniteQuery`.
88usePrefetchInfiniteQuery(projectsOptions)
89</script>
90β
91<template>
92 <Projects />
93</template>
94```