function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options): Omit<UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, "queryFn"> & object & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;Defined in: preact-query/src/queryOptions.ts:130
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.
This overload is selected when initialData is set, so the resulting data is never undefined.
TQueryFnData = unknown
TError = Error
TData = TQueryFnData
TQueryKey extends readonly unknown[] = readonly unknown[]
DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
The DefinedInitialDataOptions to use — everything you can pass to useQuery, with initialData set.
The same options object, typed so that queryKey carries the inferred data type.
import { queryOptions, useQuery } from '@tanstack/preact-query'
export const postsOptions = queryOptions({
queryKey: ['posts'],
queryFn: fetchPosts,
initialData: [],
})
function Posts() {
// `data` is `Post[]`, never `undefined`, thanks to `initialData`.
const { data } = useQuery(postsOptions)
return <>{data.map((post) => <p key={post.id}>{post.title}</p>)}</>
}function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options): OmitKeyof<UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, "queryFn"> & object & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;Defined in: preact-query/src/queryOptions.ts:199
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.
TQueryFnData = unknown
TError = Error
TData = TQueryFnData
TQueryKey extends readonly unknown[] = readonly unknown[]
UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey>
The UnusedSkipTokenOptions to use — everything you can pass to useQuery.
The same options object, typed so that queryKey carries the inferred data type.
import { queryOptions } from '@tanstack/preact-query'
export const postsOptions = queryOptions({
queryKey: ['posts'],
queryFn: fetchPosts,
})A parameterized factory, reused across a hook and an imperative call with the same cache entry:
import { noop, queryOptions, useQuery } from '@tanstack/preact-query'
export const postOptions = (id: string) =>
queryOptions({
queryKey: ['post', id],
queryFn: () => fetchPost(id),
})
function Post({ id }: { id: string }) {
const { data } = useQuery(postOptions(id))
return <h1>{data?.title}</h1>
}
// Elsewhere, e.g. to warm the cache before rendering `<Post>`:
queryClient.query(postOptions(id)).catch(noop)The same options object works with every API that accepts query options:
import {
noop,
queryOptions,
useQuery,
useSuspenseQuery,
} from '@tanstack/preact-query'
const todosOptions = queryOptions({
queryKey: ['todos'],
queryFn: fetchTodos,
})
useQuery(todosOptions)
useSuspenseQuery(todosOptions)
queryClient.query(todosOptions).catch(noop)
queryClient.getQueryData(todosOptions.queryKey) // typed as Array<Todo> | undefinedfunction queryOptions<TQueryFnData, TError, TData, TQueryKey>(options): UseQueryOptions<TQueryFnData, TError, TData, TQueryKey> & object & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;Defined in: preact-query/src/queryOptions.ts:268
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.
TQueryFnData = unknown
TError = Error
TData = TQueryFnData
TQueryKey extends readonly unknown[] = readonly unknown[]
UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
The UndefinedInitialDataOptions to use — everything you can pass to useQuery.
The same options object, typed so that queryKey carries the inferred data type.
import { queryOptions } from '@tanstack/preact-query'
export const postsOptions = queryOptions({
queryKey: ['posts'],
queryFn: fetchPosts,
})A parameterized factory, reused across a hook and an imperative call with the same cache entry:
import { noop, queryOptions, useQuery } from '@tanstack/preact-query'
export const postOptions = (id: string) =>
queryOptions({
queryKey: ['post', id],
queryFn: () => fetchPost(id),
})
function Post({ id }: { id: string }) {
const { data } = useQuery(postOptions(id))
return <h1>{data?.title}</h1>
}
// Elsewhere, e.g. to warm the cache before rendering `<Post>`:
queryClient.query(postOptions(id)).catch(noop)The same options object works with every API that accepts query options:
import {
noop,
queryOptions,
useQuery,
useSuspenseQuery,
} from '@tanstack/preact-query'
const todosOptions = queryOptions({
queryKey: ['todos'],
queryFn: fetchTodos,
})
useQuery(todosOptions)
useSuspenseQuery(todosOptions)
queryClient.query(todosOptions).catch(noop)
queryClient.getQueryData(todosOptions.queryKey) // typed as Array<Todo> | undefined