You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* feat: useSuspenseQuery
* feat: infiniteQueryOptions
* fix: add exports
* feat: useSuspenseInfiniteQuery
* feat: initialData overloads for useInfiniteQuery
* fix: types
* chore: stabilize test
we sometimes get failureCount: 2, but it doesn't matter here (timing issue)
* fix: types for useSuspenseQuery (#5755)
* docs: suspense
* docs: api reference
* docs: useSuspenseQuery in examples
* fix: types for useSuspenseInfiniteQuery (#5766)
---------
Co-authored-by: Jonghyeon Ko <[email protected]>
Copy file name to clipboardExpand all lines: docs/react/guides/suspense.md
+44-3
Original file line number
Diff line number
Diff line change
@@ -3,9 +3,7 @@ id: suspense
3
3
title: Suspense
4
4
---
5
5
6
-
> NOTE: Suspense mode for React Query is experimental, same as Suspense for data fetching itself. These APIs WILL change and should not be used in production unless you lock both your React and React Query versions to patch-level versions that are compatible with each other.
7
-
8
-
React Query can also be used with React's new Suspense for Data Fetching API's. To enable this mode, you can set either the global or query level config's `suspense` option to `true`.
6
+
React Query can also be used with React's Suspense for Data Fetching API's. To enable this mode, you can set either the global or query level config's `suspense` option to `true`.
9
7
10
8
Global configuration:
11
9
@@ -98,10 +96,53 @@ const App: React.FC = () => {
98
96
}
99
97
```
100
98
99
+
## useSuspenseQuery
100
+
101
+
You can also use the dedicated `useSuspenseQuery` hook to enable suspense mode for a query:
const { data } =useSuspenseQuery({ queryKey, queryFn })
107
+
```
108
+
109
+
This has the same effect as setting the `suspense` option to `true` in the query config, but it works better in TypeScript, because `data` is guaranteed to be defined (as errors and loading states are handled by Suspense- and ErrorBoundaries).
110
+
111
+
On the flip side, you therefore can't conditionally enable / disable the Query. `placeholderData` also doesn't exist for this Query. To prevent the UI from being replaced by a fallback during an update, wrap your updates that change the QueryKey into [startTransition](https://react.dev/reference/react/Suspense#preventing-unwanted-fallbacks).
112
+
101
113
## Fetch-on-render vs Render-as-you-fetch
102
114
103
115
Out of the box, React Query in `suspense` mode works really well as a **Fetch-on-render** solution with no additional configuration. This means that when your components attempt to mount, they will trigger query fetching and suspend, but only once you have imported them and mounted them. If you want to take it to the next level and implement a **Render-as-you-fetch** model, we recommend implementing [Prefetching](../guides/prefetching) on routing callbacks and/or user interactions events to start loading queries before they are mounted and hopefully even before you start importing or mounting their parent components.
104
116
117
+
## Suspense on the Server with streaming
118
+
119
+
If you are using `NextJs`, you can use our **experimental** integration for Suspense on the Server: `@tanstack/react-query-next-experimental`. This package will allow you to fetch data on the server (in a client component) by just calling `useQuery` (with `suspense: true`) or `useSuspenseQuery` in your component. Results will then be streamed from the server to the client as SuspenseBoundaries resolve.
120
+
121
+
To achieve this, wrap your app in the `ReactQueryStreamedHydration` component:
0 commit comments