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