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: usePrefetchQuery
* refactor: switch to actual prefetching
* refactor: remove ensureInfiniteQueryData function
will do in a separate PR
* chore: add tests for usePrefetchQuery and usePrefetchInfiniteQuery (#7586)
* chore: add tests for usePrefetchQuery and usePrefetchInfiniteQuery
* chore: update tests to assert the alternative spy is not called
* chore: add some new tests
* chore: remove it.only whoops
* chore: call mockClear after fetching
* chore: improve waterfall test by asserting fallback calls instead of loading node query
* chore: improve code repetition
* chore: add some generics to helper functions
* usePrefetchQuery type tests and docs (#7592)
* chore: add type tests and docs
* chore: update hooks to use FetchQueryOptions and FetchInfiniteQueryOptions
* chore: update tests
* chore: update docs
* chore: remove .md extension from link
* chore: add unknown default value to TQueryFnData
* Apply suggestions from code review
---------
Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
* Apply suggestions from code review
Co-authored-by: Fredrik Höglund <fredrik.hoglund@gmail.com>
* chore: fix types in tests
* chore: add new tests (#7614)
* chore: add new tests
* Apply suggestions from code review
---------
Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
---------
Co-authored-by: Bruno Lopes <88719327+brunolopesr@users.noreply.github.com>
Co-authored-by: Fredrik Höglund <fredrik.hoglund@gmail.com>
Copy file name to clipboardexpand all lines: docs/framework/react/guides/prefetching.md
+30-33
Original file line number
Diff line number
Diff line change
@@ -196,45 +196,41 @@ This starts fetching `'article-comments'` immediately and flattens the waterfall
196
196
197
197
[//]: #'Suspense'
198
198
199
-
If you want to prefetch together with Suspense, you will have to do things a bit differently. You can't use `useSuspenseQueries` to prefetch, since the prefetch would block the component from rendering. You also can not use `useQuery` for the prefetch, because that wouldn't start the prefetch until after suspenseful query had resolved. What you can do is add a small `usePrefetchQuery` function (we might add this to the library itself at a later point):
200
-
201
-
```tsx
202
-
function usePrefetchQuery(options) {
203
-
const queryClient =useQueryClient()
204
-
205
-
// This happens in render, but is safe to do because ensureQueryData
206
-
// only fetches if there is no data in the cache for this query. This
207
-
// means we know no observers are watching the data so the side effect
208
-
// is not observable, which is safe.
209
-
if (!queryClient.getQueryState(options.queryKey)) {
This approach works with both `useQuery` and `useSuspenseQuery`, so feel free to use it as an alternative to the `useQuery({ ..., notifyOnChangeProps: [] })` approach as well. The only tradeoff is that the above function will never fetch and _update_ existing data in the cache if it's stale, but this will usually happen in the later query anyway.
199
+
If you want to prefetch together with Suspense, you will have to do things a bit differently. You can't use `useSuspenseQueries` to prefetch, since the prefetch would block the component from rendering. You also can not use `useQuery` for the prefetch, because that wouldn't start the prefetch until after suspenseful query had resolved. For this scenario, you can use the [`usePrefetchQuery`](../reference/usePrefetchQuery) or the [`usePrefetchInfiniteQuery`](../reference/usePrefetchInfiniteQuery) hooks available in the library.
218
200
219
201
You can now use `useSuspenseQuery` in the component that actually needs the data. You _might_ want to wrap this later component in its own `<Suspense>` boundary so the "secondary" query we are prefetching does not block rendering of the "primary" data.
Another way is to prefetch inside of the query function. This makes sense if you know that every time an article is fetched it's very likely comments will also be needed. For this, we'll use `queryClient.prefetchQuery`:
@@ -269,6 +265,7 @@ useEffect(() => {
269
265
270
266
To recap, if you want to prefetch a query during the component lifecycle, there are a few different ways to do it, pick the one that suits your situation best:
271
267
268
+
- Prefetch before a suspense boundary using `usePrefetchQuery` or `usePrefetchInfiniteQuery` hooks
272
269
- Use `useQuery` or `useSuspenseQueries` and ignore the result
You can pass everything to `usePrefetchInfiniteQuery` that you can pass to [`queryClient.prefetchInfiniteQuery`](../../../reference/QueryClient#queryclientprefetchinfinitequery). Remember that some of them are required as below:
-**Required, but only if no default query function has been defined** See [Default Query Function](../../guides/default-query-function) for more information.
22
+
23
+
-`initialPageParam: TPageParam`
24
+
25
+
-**Required**
26
+
- The default page param to use when fetching the first page.
- When new data is received for this query, this function receives both the last page of the infinite list of data and the full array of all pages, as well as pageParam information.
32
+
- It should return a **single variable** that will be passed as the last optional parameter to your query function.
33
+
- Return `undefined` or `null` to indicate there is no next page available.
34
+
35
+
-**Returns**
36
+
37
+
The `usePrefetchInfiniteQuery` 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 [`useSuspenseInfiniteQuery`](../reference/useSuspenseInfiniteQuery)
You can pass everything to `usePrefetchQuery` that you can pass to [`queryClient.prefetchQuery`](../../../reference/QueryClient#queryclientprefetchquery). Remember that some of them are required as below:
-**Required, but only if no default query function has been defined** See [Default Query Function](../../guides/default-query-function) for more information.
21
+
22
+
**Returns**
23
+
24
+
The `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`](../reference/useSuspenseQuery).
0 commit comments