0
Fork 0
mirror of https://github.com/TanStack/query.git synced 2026-11-16 22:50:20 +00:00
query/docs/framework/react/reference/functions/usePrefetchQuery.md
Wonsuk Choi e57f8163b1
docs(query-core): add JSDoc across the package and expose it in generated reference docs (#11438)
* docs(query-core): add JSDoc across the package and expose it in generated reference docs

* docs(config.json): add sidebar entries for newly generated query-core reference docs

* docs(query-core): migrate remaining prose from the legacy 'docs/reference' pages into JSDoc

* docs(query-core): remove legacy 'docs/reference' pages superseded by the generated reference

* docs(query-core): regenerate reference docs after merging main

* docs(query-core): document missing default values for MutationOptions, combine, and QueryFilters.type

* fix(query-devtools): drop unnecessary 'query-core' project reference

* docs(query-core): document missing default values and normalize 'Defaults to' spacing

* docs(query-core): fix 'environmentManager' JSDoc example
2026-09-07 18:22:38 +02:00

2.1 KiB

id title redirect_from
usePrefetchQuery usePrefetchQuery
framework/react/reference/usePrefetchQuery
function usePrefetchQuery<TQueryFnData, TError, TData, TQueryData, TQueryKey>(options, queryClient?): void;

Defined in: packages/react-query/src/usePrefetchQuery.tsx:42

usePrefetchQuery does not return anything, it should be used just to fire a prefetch during render, before a suspense boundary that wraps a component that uses useSuspenseQuery. You can pass everything to usePrefetchQuery that you can pass to queryClient.query, though queryKey is always required, and queryFn is required unless a default query function has been defined.

The prefetch is skipped if the query already has any cached state — including a pending/error state left over from a previous attempt — so calling this on every render is cheap and won't refetch data that's already there or already in flight.

Type Parameters

TQueryFnData

TQueryFnData = unknown

TError

TError = Error

TData

TData = TQueryFnData

TQueryData

TQueryData = TQueryFnData

TQueryKey

TQueryKey extends readonly unknown[] = readonly unknown[]

Parameters

options

UsePrefetchQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>

The UsePrefetchQueryOptions to use — everything you can pass to queryClient.query.

queryClient?

QueryClient

Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.

Returns

void

void — nothing is returned.

Example

import { Suspense } from 'react'
import { usePrefetchQuery } from '@tanstack/react-query'

function App() {
  // Fire the prefetch during render, before the suspense boundary below.
  usePrefetchQuery({
    queryKey: ['posts'],
    queryFn: fetchPosts,
  })

  return (
    <Suspense fallback={<h1>Loading posts...</h1>}>
      <Posts />
    </Suspense>
  )
}