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

usePrefetchInfiniteQuery.md

96 lines3.5 KB

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