Open History to see the last commit
1---
2id: usePrefetchQuery
3title: usePrefetchQuery
4---
5β
6<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
7β
8```ts
9function usePrefetchQuery<TQueryFnData = unknown, TError = Error, TData = TQueryFnData, TQueryData = TQueryFnData, TQueryKey extends readonly unknown[] = readonly unknown[]>(options: UsePrefetchQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>, queryClient?: QueryClient): void;
10```
11β
12Defined in: [packages/preact-query/src/usePrefetchQuery.tsx:38](https://github.com/TanStack/query/blob/main/packages/preact-query/src/usePrefetchQuery.tsx#L38)
13β
14`usePrefetchQuery` does not return anything, it should be used just to fire a prefetch during render, before
15a suspense boundary that wraps a component that uses `useSuspenseQuery`. You can pass everything to
16`usePrefetchQuery` that you can pass to `queryClient.query`, though `queryKey` is always required, and
17`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 calling this on every render is cheap and won't refetch data that's
21already there or already in flight.
22β
23## Type Parameters
24β
25### TQueryFnData
26β
27`TQueryFnData` = `unknown`
28β
29### TError
30β
31`TError` = `Error`
32β
33### TData
34β
35`TData` = `TQueryFnData`
36β
37### TQueryData
38β
39`TQueryData` = `TQueryFnData`
40β
41### TQueryKey
42β
43`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
44β
45## Parameters
46β
47### options
48β
49[`UsePrefetchQueryOptions`](../type-aliases/UsePrefetchQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryData`, `TQueryKey`\>
50β
51The [UsePrefetchQueryOptions](../type-aliases/UsePrefetchQueryOptions.md) to use β everything you can pass to `queryClient.query`.
52β
53<a id="options-properties"></a>
54β
55#### `options` properties
56β
57Built from [`QueryExecuteOptions`](../interfaces/QueryExecuteOptions.md#properties). See the type above for what it changes.
58β
59### queryClient?
60β
61[`QueryClient`](../classes/QueryClient.md)
62β
63Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
64be used.
65β
66## Returns
67β
68`void`
69β
70## Example
71β
72```tsx
73import { Suspense } from 'preact/compat'
74import { usePrefetchQuery } from '@tanstack/preact-query'
75β
76function App() {
77 // Fire the prefetch during render, before the suspense boundary below.
78 usePrefetchQuery({
79 queryKey: ['posts'],
80 queryFn: fetchPosts,
81 })
82β
83 return (
84 <Suspense fallback={<h1>Loading posts...</h1>}>
85 <Posts />
86 </Suspense>
87 )
88}
89```