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

usePrefetchQuery.md

93 lines2.9 KB

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