Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

usePrefetchInfiniteQuery.md

94 lines3.3 KB

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```