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