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