useSuspenseQuery
TheuseSuspenseQuery 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
The type of data returned by the query function
The type of error that can be thrown by the query function
The type of data returned by the select function (if provided), otherwise same as TQueryFnData
The type of the query key
Parameters
Configuration options for the suspense query. Similar to
UseQueryOptions but with some differences:A unique key for the query. Must be an array.
The function that will be called to fetch data. Note:
skipToken is not allowed with useSuspenseQuery.Time in milliseconds after data is considered stale
Time in milliseconds that unused/inactive cache data remains in memory
If set to a number, the query will continuously refetch at this frequency in milliseconds
If set to
true, the query will continue to refetch while the window is in the backgroundIf set to
true, the query will refetch on window focus if the data is staleIf set to
true, the query will refetch on mount if the data is staleIf set to
true, the query will refetch on reconnect if the data is staleNumber of retry attempts or function to determine if a request should be retried
Function that receives a retry attempt number and returns the delay to apply before the next attempt
Function to transform or select a part of the data returned by the query function
Set this to
false to disable structural sharing between query resultsControls when the query function is allowed to execute
If set, the component will only re-render when one of the listed properties change
Optional metadata that can be used by query client plugins
Optional QueryClient instance to use. If not provided, the client from the nearest
QueryClientProvider will be used.Returns
The result is similar touseQuery but with guaranteed data:
The data returned by the query function. Always defined (never
undefined) once the component renders.The error object for the query, if an error occurred
The status of the query. Will be
success when the component renders (after suspense resolves).The fetch status of the query
Will be
true when the component renders (after suspense resolves)Derived from
status. Will be true if the query is in error statusWill be
true whenever the query is fetching (including background refetching)Will be
true whenever the query is refetching (fetching while data already exists)Will be
true if the data in the cache is invalidated or if the data is older than the given staleTimerefetch
(options?: { throwOnError?: boolean, cancelRefetch?: boolean }) => Promise<UseSuspenseQueryResult>
Function to manually refetch the query
Timestamp of when the data was last updated
Timestamp of when the error was last updated
The failure count for the query
The failure reason for the query retry
Differences from useQuery
- Suspends rendering: The component will suspend (show fallback) while data is being fetched initially
- Data always defined: The
dataproperty is always defined (neverundefined) when the component renders - No
enabledoption: Queries always run (cannot be disabled) - No
placeholderData: Placeholder data is not supported - No
throwOnErroroption: Errors are always thrown to the nearest Error Boundary - No
skipToken: Cannot conditionally skip the query - No
isPendingstate: The component won’t render until data is available - 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
- Always wrap with Suspense: Components using
useSuspenseQuerymust be wrapped with aSuspenseboundary - Use Error Boundaries: Wrap with an Error Boundary to handle errors gracefully
- Consider granular boundaries: Use multiple Suspense boundaries for better UX when loading different parts of the UI
- Prefetch data: Consider prefetching data before rendering to avoid showing loading states
- Not for conditional queries: Use
useQuerywithenabledif you need conditional fetching