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
* docs: suspense
* feat: remove suspense boolean from useQuery and useInfiniteQuery
* feat: useSuspenseQueries
* docs: useSuspenseQueries
* test: fix suspense in tests
* docs: offline caching is not experimental
* tests: there is no throwOnError on useSuspenseQueries
* chore: exports
* chore: try to stabilize a flaky test
Copy file name to clipboardExpand all lines: docs/react/guides/migrating-to-v5.md
+17-1Lines changed: 17 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -400,7 +400,7 @@ The `loading` status has been renamed to `pending`, and similarly the derived `i
400
400
401
401
For mutations as well the `status` has been changed from `loading` to `pending` and the `isLoading` flag has been changed to `isPending`.
402
402
403
-
Lastly the a new derived `isLoading` flag has been added to the queries that is implemented as `isPending && isFetching`. This means that `isLoading` and `isInitialLoading` have the same thing, but `isInitialLoading` is deprecated now and will be removed in the next major version.
403
+
Lastly, a new derived `isLoading` flag has been added to the queries that is implemented as `isPending && isFetching`. This means that `isLoading` and `isInitialLoading` have the same thing, but `isInitialLoading` is deprecated now and will be removed in the next major version.
404
404
405
405
To understand the reasoning behing this change checkout the [v5 roadmap discussion](https://github.com/TanStack/query/discussions/4252).
406
406
@@ -462,4 +462,20 @@ See the [TypeScript docs](../typescript#typing-query-options) for more details.
462
462
463
463
See the [useQueries docs](../reference/useQueries#combine) for more details.
464
464
465
+
### new hooks for suspense
466
+
467
+
With v5, suspense for data fetching finally becomes "stable". We've added dedicated `useSuspenseQuery`, `useSuspenseInfiniteQuery` and `useSuspenseQueries` hooks. With these hooks, `data` will never be potentially `undefined` on type level:
468
+
469
+
```js
470
+
const [post] =useSuspenseQuery({
471
+
// ^? const post: Post
472
+
queryKey: ['post', postId],
473
+
queryFn: () =>fetchPost(postId),
474
+
})
475
+
```
476
+
477
+
The experimental `suspense: boolean` flag on the query hooks has been removed.
478
+
479
+
You can read more about them in the [suspense docs](../guides/suspense).
Copy file name to clipboardExpand all lines: docs/react/guides/parallel-queries.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@ function App () {
24
24
[//]: #'Example'
25
25
[//]: #'Info'
26
26
27
-
> When using React Query in suspense mode, this pattern of parallelism does not work, since the first query would throw a promise internally and would suspend the component before the other queries run. To get around this, you'll either need to use the `useQueries` hook (which is suggested) or orchestrate your own parallelism with separate components for each `useQuery` instance (which is lame).
27
+
> When using React Query in suspense mode, this pattern of parallelism does not work, since the first query would throw a promise internally and would suspend the component before the other queries run. To get around this, you'll either need to use the `useSuspenseQueries` hook (which is suggested) or orchestrate your own parallelism with separate components for each `useSuspenseQuery` instance.
Copy file name to clipboardExpand all lines: docs/react/guides/suspense.md
+11-41Lines changed: 11 additions & 41 deletions
Original file line number
Diff line number
Diff line change
@@ -3,43 +3,27 @@ id: suspense
3
3
title: Suspense
4
4
---
5
5
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`.
6
+
React Query can also be used with React's Suspense for Data Fetching API's. For this, we have dedicated hooks:
When using suspense mode, `status` states and `error` objects are not needed and are then replaced by usage of the `React.Suspense` component (including the use of the `fallback` prop and React error boundaries for catching errors). Please read the [Resetting Error Boundaries](#resetting-error-boundaries) and look at the [Suspense Example](https://codesandbox.io/s/github/tannerlinsley/react-query/tree/main/examples/react/suspense) for more information on how to set up suspense mode.
21
13
22
-
function Root() {
23
-
return (
24
-
<QueryClientProviderclient={queryClient}>
25
-
<App />
26
-
</QueryClientProvider>
27
-
)
28
-
}
29
-
```
14
+
In addition to queries behaving differently in suspense mode, mutations also behave a bit differently. By default, instead of supplying the `error` variable when a mutation fails, it will be thrown during the next render of the component it's used in and propagate to the nearest error boundary, similar to query errors. If you wish to disable this, you can set the `throwOnError` option to `false`. If you wish that errors are not thrown at all, you can set the `throwOnError` option to `false` as well!
const { data } =useSuspenseQuery({ queryKey, queryFn })
38
22
```
39
23
40
-
When using suspense mode, `status` states and `error` objects are not needed and are then replaced by usage of the `React.Suspense` component (including the use of the `fallback` prop and React error boundaries for catching errors). Please read the [Resetting Error Boundaries](#resetting-error-boundaries) and look at the [Suspense Example](https://codesandbox.io/s/github/tannerlinsley/react-query/tree/main/examples/react/suspense) for more information on how to set up suspense mode.
24
+
This works nicely in TypeScript, because `data` is guaranteed to be defined (as errors and loading states are handled by Suspense- and ErrorBoundaries).
41
25
42
-
In addition to queries behaving differently in suspense mode, mutations also behave a bit differently. By default, instead of supplying the `error` variable when a mutation fails, it will be thrown during the next render of the component it's used in and propagate to the nearest error boundary, similar to query errors. If you wish to disable this, you can set the `throwOnError` option to `false`. If you wish that errors are not thrown at all, you can set the `throwOnError` option to `false` as well!
26
+
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).
43
27
44
28
## Resetting Error Boundaries
45
29
@@ -96,20 +80,6 @@ const App: React.FC = () => {
96
80
}
97
81
```
98
82
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
-
113
83
## Fetch-on-render vs Render-as-you-fetch
114
84
115
85
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.
0 commit comments