Skip to main content

useSuspenseQuery

The useSuspenseQuery hook is designed to work with React Suspense. It suspends rendering while data is being fetched and always returns defined data (never undefined).

Import

Signature

Type Parameters

type
default:"unknown"
The type of data returned by the query function
type
default:"DefaultError"
The type of error that can be thrown by the query function
type
default:"TQueryFnData"
The type of data returned by the select function (if provided), otherwise same as TQueryFnData
type
default:"QueryKey"
The type of the query key

Parameters

UseSuspenseQueryOptions
required
Configuration options for the suspense query. Similar to UseQueryOptions but with some differences:
TQueryKey
required
A unique key for the query. Must be an array.
QueryFunction<TQueryFnData, TQueryKey>
required
The function that will be called to fetch data. Note: skipToken is not allowed with useSuspenseQuery.
number | ((query: Query) => number)
default:"0"
Time in milliseconds after data is considered stale
number
default:"300000"
Time in milliseconds that unused/inactive cache data remains in memory
number | false | ((query: Query) => number | false)
default:"false"
If set to a number, the query will continuously refetch at this frequency in milliseconds
boolean
default:"false"
If set to true, the query will continue to refetch while the window is in the background
boolean | 'always' | ((query: Query) => boolean | 'always')
default:"true"
If set to true, the query will refetch on window focus if the data is stale
boolean | 'always' | ((query: Query) => boolean | 'always')
default:"true"
If set to true, the query will refetch on mount if the data is stale
boolean | 'always' | ((query: Query) => boolean | 'always')
default:"true"
If set to true, the query will refetch on reconnect if the data is stale
boolean | number | (failureCount: number, error: TError) => boolean
default:"3"
Number of retry attempts or function to determine if a request should be retried
number | (retryAttempt: number, error: TError) => number
Function that receives a retry attempt number and returns the delay to apply before the next attempt
(data: TQueryFnData) => TData
Function to transform or select a part of the data returned by the query function
boolean | (oldData: unknown, newData: unknown) => unknown
default:"true"
Set this to false to disable structural sharing between query results
'online' | 'always' | 'offlineFirst'
default:"'online'"
Controls when the query function is allowed to execute
Array<keyof UseSuspenseQueryResult> | 'all'
If set, the component will only re-render when one of the listed properties change
Record<string, unknown>
Optional metadata that can be used by query client plugins
QueryClient
Optional QueryClient instance to use. If not provided, the client from the nearest QueryClientProvider will be used.

Returns

The result is similar to useQuery but with guaranteed data:
TData
The data returned by the query function. Always defined (never undefined) once the component renders.
TError | null
The error object for the query, if an error occurred
'pending' | 'error' | 'success'
The status of the query. Will be success when the component renders (after suspense resolves).
'fetching' | 'paused' | 'idle'
The fetch status of the query
boolean
Will be true when the component renders (after suspense resolves)
boolean
Derived from status. Will be true if the query is in error status
boolean
Will be true whenever the query is fetching (including background refetching)
boolean
Will be true whenever the query is refetching (fetching while data already exists)
boolean
Will be true if the data in the cache is invalidated or if the data is older than the given staleTime
(options?: { throwOnError?: boolean, cancelRefetch?: boolean }) => Promise<UseSuspenseQueryResult>
Function to manually refetch the query
number
Timestamp of when the data was last updated
number
Timestamp of when the error was last updated
number
The failure count for the query
TError | null
The failure reason for the query retry

Differences from useQuery

  1. Suspends rendering: The component will suspend (show fallback) while data is being fetched initially
  2. Data always defined: The data property is always defined (never undefined) when the component renders
  3. No enabled option: Queries always run (cannot be disabled)
  4. No placeholderData: Placeholder data is not supported
  5. No throwOnError option: Errors are always thrown to the nearest Error Boundary
  6. No skipToken: Cannot conditionally skip the query
  7. No isPending state: The component won’t render until data is available
  8. No isPlaceholderData: Not included in result type

Examples

Basic Usage

With Type Safety

With Error Boundary

Multiple Suspense Queries

With Data Transformation

Nested Suspense Boundaries

Best Practices

  1. Always wrap with Suspense: Components using useSuspenseQuery must be wrapped with a Suspense boundary
  2. Use Error Boundaries: Wrap with an Error Boundary to handle errors gracefully
  3. Consider granular boundaries: Use multiple Suspense boundaries for better UX when loading different parts of the UI
  4. Prefetch data: Consider prefetching data before rendering to avoid showing loading states
  5. Not for conditional queries: Use useQuery with enabled if you need conditional fetching

Source

Implementation: useSuspenseQuery.ts:8