Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TanStack · Function

queryOptions

queryOptions builds reusable, type-safe TanStack Query configurations. The returned options work with hooks and imperative QueryClient APIs while carrying query data and error types through the query key.

How it works

queryOptions packages a query key and its behavior into a reusable, type-aware options object. Its returned value brands the key with the query function's data and error types, allowing the same configuration to work with hooks and imperative QueryClient methods while preserving inference.

Syntax

function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;

Parameters

NameTypeDescription
optionsDefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> | UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey> | UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>A query options object, including the required queryKey and the query behavior accepted by useQuery.

Returns

The supplied query options augmented with a QueryKeyWithDataTag carrying the query function's data and error types.

Key points

  • One options definition can serve both hook-based reads and imperative client operations.
  • The API is generic over query-function data, error, selected data, and query-key types.
  • When initialData is supplied, the resulting option type reflects defined initial data.
  • The query key identifies the cache entry generated by the options.

Common mistakes

  • A queryKey must be supplied; it is not optional.
  • The function accepts the normal useQuery option set, but the options still need to match the selected framework's API.

Example: Reusable typed query options

import { queryOptions, useQuery } from '@tanstack/react-query'

type Todo = { id: number; title: string }

async function fetchTodos(): Promise<Todo[]> {
  const response = await fetch('/api/todos')
  if (!response.ok) throw new Error('Request failed')
  return response.json()
}

const todosOptions = queryOptions({
  queryKey: ['todos'],
  queryFn: fetchTodos,
})

export function Todos() {
  const { data = [] } = useQuery(todosOptions)
  return <ul>{data.map(todo => <li key={todo.id}>{todo.title}</li>)}</ul>
}

Define one typed options object and reuse it with a query hook or imperative QueryClient API.

When to use it

Centralize query configuration

Several components use the same resource query and should share one cache definition.

Reuse options across APIs

A feature needs both a React hook and an imperative QueryClient operation.

Preserve type inference

A TypeScript codebase needs query-key-based inference for fetched and selected data.

Interview questions

QWhat problem does queryOptions solve?

queryOptions creates a reusable query configuration from options such as queryKey and queryFn. The returned object carries query-function data and error types through the query key, improving inference when reused.

Follow-up: Why is that useful beyond hooks? The same options can be passed to imperative QueryClient APIs, so one definition can coordinate declarative and imperative access.

QWhat does queryOptions accept and return?

Its options parameter is a QueryOptions-shaped object, and queryKey is required. The function returns the corresponding options object with a typed query-key data tag.

Follow-up: What must remain consistent when reusing it? The query key and query function should describe the same resource so cache identity and inferred data remain aligned.

QWhere can the returned options be used?

A queryOptions result can generally be passed to useQuery and to imperative APIs such as queryClient.query. This makes the query definition reusable instead of duplicating its key and fetch behavior.

Follow-up: How would you share a parameterized query? Create a function that accepts the parameter and returns queryOptions with that parameter included in queryKey and used by queryFn.

  • useQuery
  • QueryClient
  • queryClient.query
  • QueryKeyWithDataTag
  • DefinedInitialDataOptions

Source: queryOptions | TanStack Query React Docs. Checked 2026-10-15.