- React Reference
- Functions / HydrationBoundary
- Functions / QueryClientProvider
- Functions / QueryErrorResetBoundary
- Functions / defaultShouldDehydrateMutation
- Functions / defaultShouldDehydrateQuery
- Functions / dehydrate
- Functions / dehydrateQuery
- Functions / experimental_streamedQuery
- Functions / hashKey
- Functions / hydrate
- Functions / infiniteQueryOptions
- Functions / isCancelledError
- Functions / keepPreviousData
- Functions / matchMutation
- Functions / matchQuery
- Functions / mutationOptions
- Functions / noop
- Functions / partialMatchKey
- Functions / queryOptions
- Functions / replaceEqualDeep
- Functions / shouldThrowError
- Functions / useInfiniteQuery
- Functions / useIsFetching
- Functions / useIsMutating
- Functions / useIsRestoring
- Functions / useMutation
- Functions / useMutationState
- Functions / usePrefetchInfiniteQuery
- Functions / usePrefetchQuery
- Functions / useQueries
- Functions / useQuery
- Functions / useQueryClient
- Functions / useQueryErrorResetBoundary
- Functions / useSuspenseInfiniteQuery
- Functions / useSuspenseQueries
- Functions / useSuspenseQuery
- Classes / CancelledError
- Classes / InfiniteQueryObserver
- Classes / Mutation
- Classes / MutationCache
- Classes / MutationObserver
- Classes / QueriesObserver
- Classes / Query
- Classes / QueryCache
- Classes / QueryClient
- Classes / QueryObserver
- Interfaces / CancelOptions
- Interfaces / DefaultOptions
- Interfaces / DehydrateOptions
- Interfaces / DehydratedState
- Interfaces / EnsureQueryDataOptions
- Interfaces / FetchNextPageOptions
- Interfaces / FetchPreviousPageOptions
- Interfaces / FetchQueryOptions
- Interfaces / FocusManager
- Interfaces / HydrateOptions
- Interfaces / HydrationBoundaryProps
- Interfaces / InfiniteData
- Interfaces / InfiniteQueryObserverBaseResult
- Interfaces / InfiniteQueryObserverLoadingErrorResult
- Interfaces / InfiniteQueryObserverLoadingResult
- Interfaces / InfiniteQueryObserverOptions
- Interfaces / InfiniteQueryObserverPendingResult
- Interfaces / InfiniteQueryObserverPlaceholderResult
- Interfaces / InfiniteQueryObserverRefetchErrorResult
- Interfaces / InfiniteQueryObserverSuccessResult
- Interfaces / InfiniteQueryPageParamsOptions
- Interfaces / InitialPageParam
- Interfaces / InvalidateOptions
- Interfaces / InvalidateQueryFilters
- Interfaces / MutateOptions
- Interfaces / MutationCacheConfig
- Interfaces / MutationFilters
- Interfaces / MutationObserverBaseResult
- Interfaces / MutationObserverErrorResult
- Interfaces / MutationObserverIdleResult
- Interfaces / MutationObserverLoadingResult
- Interfaces / MutationObserverOptions
- Interfaces / MutationObserverSuccessResult
- Interfaces / MutationOptions
- Interfaces / MutationState
- Interfaces / NotifyEvent
- Interfaces / OnlineManager
- Interfaces / QueriesObserverOptions
- Interfaces / QueryCacheConfig
- Interfaces / QueryClientConfig
- Interfaces / QueryErrorResetBoundaryProps
- Interfaces / QueryExecuteOptions
- Interfaces / QueryFilters
- Interfaces / QueryObserverBaseResult
- Interfaces / QueryObserverLoadingErrorResult
- Interfaces / QueryObserverLoadingResult
- Interfaces / QueryObserverOptions
- Interfaces / QueryObserverPendingResult
- Interfaces / QueryObserverPlaceholderResult
- Interfaces / QueryObserverRefetchErrorResult
- Interfaces / QueryObserverSuccessResult
- Interfaces / QueryOptions
- Interfaces / QueryState
- Interfaces / RefetchOptions
- Interfaces / RefetchQueryFilters
- Interfaces / Register
- Interfaces / ResetOptions
- Interfaces / ResultOptions
- Interfaces / SetDataOptions
- Interfaces / TimeoutManager
- Interfaces / UseBaseQueryOptions
- Interfaces / UseInfiniteQueryOptions
- Interfaces / UseMutationOptions
- Interfaces / UseQueryOptions
- Interfaces / UseSuspenseInfiniteQueryOptions
- Interfaces / UseSuspenseQueryOptions
- Types / AnyDataTag
- Types / AnyUseBaseQueryOptions
- Types / AnyUseInfiniteQueryOptions
- Types / AnyUseMutationOptions
- Types / AnyUseQueryOptions
- Types / AnyUseSuspenseInfiniteQueryOptions
- Types / AnyUseSuspenseQueryOptions
- Types / DataTag
- Types / DefaultError
- Types / DefaultedInfiniteQueryObserverOptions
- Types / DefaultedQueryObserverOptions
- Types / DefinedInfiniteQueryObserverResult
- Types / DefinedInitialDataInfiniteOptions
- Types / DefinedInitialDataOptions
- Types / DefinedQueryObserverResult
- Types / DefinedUseInfiniteQueryResult
- Types / DefinedUseQueryResult
- Types / DistributiveOmit
- Types / EnsureInfiniteQueryDataOptions
- Types / FetchInfiniteQueryOptions
- Types / FetchStatus
- Types / GetNextPageParamFunction
- Types / GetPreviousPageParamFunction
- Types / InferDataFromTag
- Types / InferErrorFromTag
- Types / InfiniteQueryExecuteOptions
- Types / InfiniteQueryObserverResult
- Types / InitialDataFunction
- Types / ManagedTimerId
- Types / MutateFunction
- Types / MutateFunctionRest
- Types / MutationCacheNotifyEvent
- Types / MutationFunction
- Types / MutationFunctionContext
- Types / MutationKey
- Types / MutationMeta
- Types / MutationObserverResult
- Types / MutationScope
- Types / MutationStatus
- Types / NetworkMode
- Types / NonUndefinedGuard
- Types / NotifyEventType
- Types / NotifyOnChangeProps
- Types / OmitKeyof
- Types / Override
- Types / PlaceholderDataFunction
- Types / QueriesOptions
- Types / QueriesPlaceholderDataFunction
- Types / QueriesResults
- Types / QueryBooleanOption
- Types / QueryCacheNotifyEvent
- Types / QueryClientProviderProps
- Types / QueryErrorClearResetFunction
- Types / QueryErrorIsResetFunction
- Types / QueryErrorResetBoundaryFunction
- Types / QueryErrorResetFunction
- Types / QueryFunction
- Types / QueryFunctionContext
- Types / QueryKey
- Types / QueryKeyHashFunction
- Types / QueryKeyWithDataTag
- Types / QueryMeta
- Types / QueryObserverResult
- Types / QueryPersister
- Types / QueryStatus
- Types / SkipToken
- Types / StaleTime
- Types / StaleTimeFunction
- Types / SuspenseQueriesOptions
- Types / SuspenseQueriesResults
- Types / ThrowOnError
- Types / TimeoutCallback
- Types / TimeoutProvider
- Types / UndefinedInitialDataInfiniteOptions
- Types / UndefinedInitialDataOptions
- Types / UnsetMarker
- Types / UnusedSkipTokenInfiniteOptions
- Types / UnusedSkipTokenOptions
- Types / Updater
- Types / UseBaseMutationResult
- Types / UseBaseQueryResult
- Types / UseInfiniteQueryResult
- Types / UseMutateAsyncFunction
- Types / UseMutateFunction
- Types / UseMutationResult
- Types / UsePrefetchInfiniteQueryOptions
- Types / UsePrefetchQueryOptions
- Types / UseQueryResult
- Types / UseSuspenseInfiniteQueryResult
- Types / UseSuspenseQueryResult
- Types / WithRequired
- Types / dataTagErrorSymbol
- Types / dataTagSymbol
- Variables / IsRestoringProvider
- Variables / QueryClientContext
- Variables / dataTagErrorSymbol
- Variables / dataTagSymbol
- Variables / defaultScheduler
- Variables / environmentManager
- Variables / focusManager
- Variables / isServer
- Variables / notifyManager
- Variables / onlineManager
- Variables / skipToken
- Variables / timeoutManager
- Variables / unsetMarker
queryOptions
Overview#
function 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>;
function 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>;
function 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>;- DefinedInitialDataOptions → DefinedInitialDataOptions & QueryKeyWithDataTag: 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.
- UnusedSkipTokenOptions → UnusedSkipTokenOptions & QueryKeyWithDataTag: 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.
- UndefinedInitialDataOptions → UndefinedInitialDataOptions & QueryKeyWithDataTag: 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.
See also: Parameters · Returns
Call Signature#
function 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>;Defined in: packages/react-query/src/queryOptions.ts:138
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 (unless a select changes TData to include undefined).
Type Parameters#
TQueryFnData#
TQueryFnData = unknown
TError#
TError = Error
TData#
TData = TQueryFnData
TQueryKey#
TQueryKey extends readonly unknown[] = readonly unknown[]
Parameters#
options#
DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
The DefinedInitialDataOptions to use — everything you can pass to useQuery, with initialData set.
Returns#
DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
The same options object, typed so that queryKey carries the inferred data type.
See#
- useQuery to run a query with these options.
- The Query Options API for more on this pattern.
Example#
import { queryOptions, useQuery } from '@tanstack/react-query'
export const postsOptions = queryOptions({
queryKey: ['posts'],
queryFn: fetchPosts,
initialData: [],
})
function Posts() {
// `data` is `Post[]`, never `undefined`, thanks to `initialData` — even if a refetch fails,
// so the list stays visible alongside the error.
const { data, isError, error } = useQuery(postsOptions)
return (
<div>
{isError ? <span>Error: {error.message}</span> : null}
<ul>
{data.map((post) => <li key={post.id}>{post.title}</li>)}
</ul>
</div>
)
}Call Signature#
function 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>;Defined in: packages/react-query/src/queryOptions.ts:177
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.
Type Parameters#
TQueryFnData#
TQueryFnData = unknown
TError#
TError = Error
TData#
TData = TQueryFnData
TQueryKey#
TQueryKey extends readonly unknown[] = readonly unknown[]
Parameters#
options#
UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey>
The UnusedSkipTokenOptions to use — everything you can pass to useQuery.
Returns#
UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
The same options object, typed so that queryKey carries the inferred data type.
See#
- useQuery to run a query with these options.
- The Query Options API for more on this pattern.
Example#
A parameterized factory, so the same options object can be reused per id:
import { queryOptions, useQuery } from '@tanstack/react-query'
export const postOptions = (id: string) =>
queryOptions({
queryKey: ['post', id],
queryFn: () => fetchPost(id),
})
function Post({ id }: { id: string }) {
const { data, isPending, isError, error } = useQuery(postOptions(id))
if (isPending) return 'Loading...'
if (isError) return <span>Error: {error.message}</span>
return <h1>{data.title}</h1>
}Call Signature#
function 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>;Defined in: packages/react-query/src/queryOptions.ts:238
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.
Type Parameters#
TQueryFnData#
TQueryFnData = unknown
TError#
TError = Error
TData#
TData = TQueryFnData
TQueryKey#
TQueryKey extends readonly unknown[] = readonly unknown[]
Parameters#
options#
UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
The UndefinedInitialDataOptions to use — everything you can pass to useQuery.
Returns#
UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
The same options object, typed so that queryKey carries the inferred data type.
Remarks#
This is the only overload that accepts queryFn: skipToken, shown below.
See#
- useQuery to run a query with these options.
- The Query Options API for more on this pattern.
Examples#
A parameterized factory, so the same options object can be reused per id:
import { queryOptions, useQuery } from '@tanstack/react-query'
export const postOptions = (id: string) =>
queryOptions({
queryKey: ['post', id],
queryFn: () => fetchPost(id),
})
function Post({ id }: { id: string }) {
const { data, isPending, isError, error } = useQuery(postOptions(id))
if (isPending) return 'Loading...'
if (isError) return <span>Error: {error.message}</span>
return <h1>{data.title}</h1>
}A factory that disables the query, type safe, until postId is set:
import { queryOptions, skipToken, useQuery } from '@tanstack/react-query'
export const postOptions = (postId: number | undefined) =>
queryOptions({
queryKey: ['post', postId],
queryFn: postId != null ? () => fetchPost(postId) : skipToken,
})
function Post({ postId }: { postId: number | undefined }) {
const { data, isLoading, isError, error } = useQuery(postOptions(postId))
if (postId == null) return 'Select a post'
if (isLoading) return 'Loading...'
if (isError) return <span>Error: {error.message}</span>
return <h1>{data?.title}</h1>
}Parameters#
options#
UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
The UndefinedInitialDataOptions to use — everything you can pass to useQuery.
options properties#
Built from UseQueryOptions. See the type above for what it changes.
Returns#
UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
The same options object, typed so that queryKey carries the inferred data type.
Result properties#
Built from UseQueryOptions, QueryKeyWithDataTag. See the type above for what it changes.