Open History to see the last commit
1---
2id: queryOptions
3title: queryOptions
4---
5β
6## Overview
7β
8```ts
9function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
10function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey>): UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
11function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
12```
13β
14- [`DefinedInitialDataOptions` β `DefinedInitialDataOptions & QueryKeyWithDataTag`](#call-signature-1): You can generally pass everything to `queryOptions` that you can also pass to `useQuery`. These options can be shared across hooks and imperative APIs such as `queryClient.query`. `options.queryKey` is required and is the query key to generate options for.
15- [`UnusedSkipTokenOptions` β `UnusedSkipTokenOptions & QueryKeyWithDataTag`](#call-signature-2): You can generally pass everything to `queryOptions` that you can also pass to `useQuery`. These options can be shared across hooks and imperative APIs such as `queryClient.query`. `options.queryKey` is required and is the query key to generate options for.
16- [`UndefinedInitialDataOptions` β `UndefinedInitialDataOptions & QueryKeyWithDataTag`](#call-signature-3): You can generally pass everything to `queryOptions` that you can also pass to `useQuery`. These options can be shared across hooks and imperative APIs such as `queryClient.query`. `options.queryKey` is required and is the query key to generate options for.
17β
18See also: [Parameters](#parameters-summary) Β· [Returns](#returns-summary)
19β
20<a id="call-signature-1"></a>
21β
22## Call Signature
23β
24```ts
25function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
26```
27β
28Defined in: [packages/preact-query/src/queryOptions.ts:139](https://github.com/TanStack/query/blob/main/packages/preact-query/src/queryOptions.ts#L139)
29β
30You can generally pass everything to `queryOptions` that you can also pass to `useQuery`. These options can
31be shared across hooks and imperative APIs such as `queryClient.query`. `options.queryKey` is required and
32is the query key to generate options for.
33β
34This overload is selected when `initialData` is set, so the resulting `data` is never `undefined` (unless
35a `select` changes `TData` to include `undefined`).
36β
37### Type Parameters
38β
39#### TQueryFnData
40β
41`TQueryFnData` = `unknown`
42β
43#### TError
44β
45`TError` = `Error`
46β
47#### TData
48β
49`TData` = `TQueryFnData`
50β
51#### TQueryKey
52β
53`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
54β
55### Parameters
56β
57#### options
58β
59[`DefinedInitialDataOptions`](../type-aliases/DefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
60β
61The [DefinedInitialDataOptions](../type-aliases/DefinedInitialDataOptions.md) to use β everything you can pass to `useQuery`, with `initialData` set.
62β
63### Returns
64β
65[`DefinedInitialDataOptions`](../type-aliases/DefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, `TQueryFnData`, `TError`\>
66β
67The same options object, typed so that `queryKey` carries the inferred data type.
68β
69### See
70β
71 - [useQuery](useQuery.md) to run a query with these options.
72 - [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern.
73β
74### Example
75β
76```tsx
77import { queryOptions, useQuery } from '@tanstack/preact-query'
78β
79export const postsOptions = queryOptions({
80 queryKey: ['posts'],
81 queryFn: fetchPosts,
82 initialData: [],
83})
84β
85function Posts() {
86 // `data` is `Post[]`, never `undefined`, thanks to `initialData` β even if a refetch fails,
87 // so the list stays visible alongside the error.
88 const { data, isError, error } = useQuery(postsOptions)
89β
90 return (
91 <div>
92 {isError ? <span>Error: {error.message}</span> : null}
93 <ul>
94 {data.map((post) => <li key={post.id}>{post.title}</li>)}
95 </ul>
96 </div>
97 )
98}
99```
100β
101<a id="call-signature-2"></a>
102β
103## Call Signature
104β
105```ts
106function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey>): UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
107```
108β
109Defined in: [packages/preact-query/src/queryOptions.ts:178](https://github.com/TanStack/query/blob/main/packages/preact-query/src/queryOptions.ts#L178)
110β
111You can generally pass everything to `queryOptions` that you can also pass to `useQuery`. These options can
112be shared across hooks and imperative APIs such as `queryClient.query`. `options.queryKey` is required and
113is the query key to generate options for.
114β
115### Type Parameters
116β
117#### TQueryFnData
118β
119`TQueryFnData` = `unknown`
120β
121#### TError
122β
123`TError` = `Error`
124β
125#### TData
126β
127`TData` = `TQueryFnData`
128β
129#### TQueryKey
130β
131`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
132β
133### Parameters
134β
135#### options
136β
137[`UnusedSkipTokenOptions`](../type-aliases/UnusedSkipTokenOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
138β
139The [UnusedSkipTokenOptions](../type-aliases/UnusedSkipTokenOptions.md) to use β everything you can pass to `useQuery`.
140β
141### Returns
142β
143[`UnusedSkipTokenOptions`](../type-aliases/UnusedSkipTokenOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, `TQueryFnData`, `TError`\>
144β
145The same options object, typed so that `queryKey` carries the inferred data type.
146β
147### See
148β
149 - [useQuery](useQuery.md) to run a query with these options.
150 - [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern.
151β
152### Example
153β
154A parameterized factory, so the same options object can be reused per `id`:
155```tsx
156import { queryOptions, useQuery } from '@tanstack/preact-query'
157β
158export const postOptions = (id: string) =>
159 queryOptions({
160 queryKey: ['post', id],
161 queryFn: () => fetchPost(id),
162 })
163β
164function Post({ id }: { id: string }) {
165 const { data, isPending, isError, error } = useQuery(postOptions(id))
166β
167 if (isPending) return 'Loading...'
168 if (isError) return <span>Error: {error.message}</span>
169β
170 return <h1>{data.title}</h1>
171}
172```
173β
174<a id="call-signature-3"></a>
175β
176## Call Signature
177β
178```ts
179function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
180```
181β
182Defined in: [packages/preact-query/src/queryOptions.ts:239](https://github.com/TanStack/query/blob/main/packages/preact-query/src/queryOptions.ts#L239)
183β
184You can generally pass everything to `queryOptions` that you can also pass to `useQuery`. These options can
185be shared across hooks and imperative APIs such as `queryClient.query`. `options.queryKey` is required and
186is the query key to generate options for.
187β
188### Type Parameters
189β
190#### TQueryFnData
191β
192`TQueryFnData` = `unknown`
193β
194#### TError
195β
196`TError` = `Error`
197β
198#### TData
199β
200`TData` = `TQueryFnData`
201β
202#### TQueryKey
203β
204`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
205β
206### Parameters
207β
208#### options
209β
210[`UndefinedInitialDataOptions`](../type-aliases/UndefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
211β
212The [UndefinedInitialDataOptions](../type-aliases/UndefinedInitialDataOptions.md) to use β everything you can pass to `useQuery`.
213β
214### Returns
215β
216[`UndefinedInitialDataOptions`](../type-aliases/UndefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, `TQueryFnData`, `TError`\>
217β
218The same options object, typed so that `queryKey` carries the inferred data type.
219β
220### Remarks
221β
222This is the only overload that accepts `queryFn: skipToken`, shown below.
223β
224### See
225β
226 - [useQuery](useQuery.md) to run a query with these options.
227 - [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern.
228β
229### Examples
230β
231A parameterized factory, so the same options object can be reused per `id`:
232```tsx
233import { queryOptions, useQuery } from '@tanstack/preact-query'
234β
235export const postOptions = (id: string) =>
236 queryOptions({
237 queryKey: ['post', id],
238 queryFn: () => fetchPost(id),
239 })
240β
241function Post({ id }: { id: string }) {
242 const { data, isPending, isError, error } = useQuery(postOptions(id))
243β
244 if (isPending) return 'Loading...'
245 if (isError) return <span>Error: {error.message}</span>
246β
247 return <h1>{data.title}</h1>
248}
249```
250β
251A factory that disables the query, type safe, until `postId` is set:
252```tsx
253import { queryOptions, skipToken, useQuery } from '@tanstack/preact-query'
254β
255export const postOptions = (postId: number | undefined) =>
256 queryOptions({
257 queryKey: ['post', postId],
258 queryFn: postId != null ? () => fetchPost(postId) : skipToken,
259 })
260β
261function Post({ postId }: { postId: number | undefined }) {
262 const { data, isLoading, isError, error } = useQuery(postOptions(postId))
263β
264 if (postId == null) return 'Select a post'
265 if (isLoading) return 'Loading...'
266 if (isError) return <span>Error: {error.message}</span>
267β
268 return <h1>{data?.title}</h1>
269}
270```
271β
272<a id="parameters-summary"></a>
273β
274## Parameters
275β
276### options
277β
278[`UndefinedInitialDataOptions`](../type-aliases/UndefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
279β
280The [UndefinedInitialDataOptions](../type-aliases/UndefinedInitialDataOptions.md) to use β everything you can pass to `useQuery`.
281β
282<a id="options-properties"></a>
283β
284#### `options` properties
285β
286Built from [`UseQueryOptions`](../interfaces/UseQueryOptions.md#properties). See the type above for what it changes.
287β
288<a id="returns-summary"></a>
289β
290## Returns
291β
292[`UndefinedInitialDataOptions`](../type-aliases/UndefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, `TQueryFnData`, `TError`\>
293β
294The same options object, typed so that `queryKey` carries the inferred data type.
295β
296<a id="result-properties"></a>
297β
298### Result properties
299β
300Built from [`UseQueryOptions`](../interfaces/UseQueryOptions.md#properties), [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md#properties). See the type above for what it changes.