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
@@ -380,9 +377,25 @@ Its available properties and methods are:
380
377
- Optional
381
378
- Define defaults for all queries and mutations using this query cache.
382
379
380
+
## `queryCache.fetchQuery`
381
+
382
+
`fetchQuery` is an asynchronous method that can be used to fetch and cache a query. It will either resolve with the data or throw with the error. Specify a `staleTime` to only trigger a fetch when the data is stale. Use the `prefetchQuery` method if you just want to fetch a query without needing the result.
`prefetchQuery` is an asynchronous function that can be used to fetch and cache a query response before it is needed or rendered with `useQuery` and friends.
398
+
`prefetchQuery` is an asynchronous method that can be used to fetch and cache a query response before it is needed or rendered with `useQuery` and friends.
386
399
387
400
- If either:
388
401
- The query does not exist or
@@ -394,13 +407,13 @@ Its available properties and methods are:
394
407
> The difference between using `prefetchQuery` and `setQueryData` is that `prefetchQuery` is async and will ensure that duplicate requests for this query are not created with `useQuery` instances for the same query are rendered while the data is fetching.
You can even use it with a default queryFn in your config!
410
423
411
424
```js
412
-
constdata=awaitqueryCache.prefetchQuery(queryKey)
425
+
awaitqueryCache.prefetchQuery(queryKey)
413
426
```
414
427
415
428
**Options**
@@ -423,7 +436,7 @@ The options for `prefetchQuery` are exactly the same as those of [`useQuery`](#u
423
436
424
437
**Returns**
425
438
426
-
- `promise:Promise`
439
+
- `Promise<TResult |undefined>`
427
440
- A promise is returned that will either immediately resolve with the query's cached response data, or resolve to the data returned by the fetch function. It **will not** throw an error if the fetch fails. This can be configured by setting the `throwOnError` option to `true`.
428
441
429
442
## `queryCache.getQueryData`
@@ -478,6 +491,52 @@ For convenience in syntax, you can also pass an updater function which receives
478
491
setQueryData(queryKey, oldData=> newData)
479
492
```
480
493
494
+
## `queryCache.refetchQueries`
495
+
496
+
The `refetchQueries` method can be used to refetch queries based on certain conditions.
- `queryKeyOrPredicateFn` can either be a [Query Key](#query-keys) or a `Function`
520
+
- `queryKey: QueryKey`
521
+
- If a query key is passed, queries will be filtered to those where this query key is included in the existing query's query key. This means that if you passed a query key of `'todos'`, it would match queries with the `todos`, `['todos']`, and `['todos', 5]`. See [Query Keys](./guides/queries#query-keys) for more information.
522
+
- `query=> boolean`
523
+
- This predicate function will be called for every single query in the cache and be expected to return truthy for queries that are `found`.
524
+
- The `exact` option has no effect when using a function
525
+
- `exact?: boolean`
526
+
- If you don't want to search queries inclusively by query key, you can pass the `exact:true` option to return only the query with the exact query key you have passed. Remember to destructure it out of the array!
527
+
- `active?: boolean`
528
+
- When set to `true` it will refetch active queries.
529
+
- When set to `false` it will refetch inactive queries.
530
+
- `stale?: boolean`
531
+
- When set to `true` it will match on stale queries.
532
+
- When set to `false` it will match on fresh queries.
533
+
- `throwOnError?: boolean`
534
+
- When set to `true`, this method will throw if any of the query refetch tasks fail.
535
+
536
+
**Returns**
537
+
538
+
This function returns a promise that will resolve when all of the queries are done being refetched. By default, it **will not** throw an error if any of those queries refetches fail, but this can be configured by setting the `throwOnError` option to `true`
539
+
481
540
## `queryCache.invalidateQueries`
482
541
483
542
The `invalidateQueries` method can be used to invalidate and refetch single or multiple queries in the cache based on their query keys or any other functionally accessible property/state of the query. By default, all matching queries are immediately marked as stale and active queries are refetched in the background.
@@ -549,7 +608,7 @@ This function does not return anything
549
608
The `removeQueries` method can be used to remove queries from the cache based on their query keys or any other functionally accessible property/state of the query.
Copy file name to clipboardExpand all lines: docs/src/pages/docs/guides/prefetching.md
+2-4Lines changed: 2 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,13 +3,11 @@ id: prefetching
3
3
title: Prefetching
4
4
---
5
5
6
-
If you're lucky enough, you may know enough about what your users will do to be able to prefetch the data they need before it's needed! If this is the case, you can use the `prefetchQuery`function to prefetch the results of a query to be placed into the cache:
6
+
If you're lucky enough, you may know enough about what your users will do to be able to prefetch the data they need before it's needed! If this is the case, you can use the `fetchQuery` or `prefetchQuery`methods to prefetch the results of a query to be placed into the cache:
Copy file name to clipboardExpand all lines: docs/src/pages/docs/guides/ssr.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -118,12 +118,12 @@ Since there are many different possible setups for SSR, it's hard to give a deta
118
118
> Note: The global `queryCache` you can import directly from 'react-query' does not cache queries on the server to avoid leaking sensitive information between requests.
119
119
120
120
- Prefetch data
121
-
- Create a `prefetchQueryCache` specifically for prefetching by calling `const prefetchQueryCache = new QueryCache()`
122
-
- Call `prefetchQueryCache.prefetchQuery(...)` to prefetch queries
123
-
- Dehydrate by using `const dehydratedState = dehydrate(prefetchQueryCache)`
121
+
- Create a `prefetchCache` specifically for prefetching by calling `const prefetchCache = new QueryCache()`
122
+
- Call `prefetchCache.prefetchQuery(...)` to prefetch queries
123
+
- Dehydrate by using `const dehydratedState = dehydrate(prefetchCache)`
124
124
- Render
125
-
- Create a new render query cache and hydrate the state. Use this query cache to render your app.
126
-
-**Do not** use the `prefetchQueryCache` to render your app, the server and client both needs to render from the dehydrated data to avoid React hydration mismatches. This is because queries with errors are excluded from dehydration by default.
125
+
- Create a new query cache for rendering and hydrate the state. Use this query cache to render your app.
126
+
-**Do not** use the `prefetchCache` to render your app, the server and client both needs to render from the dehydrated data to avoid React hydration mismatches. This is because queries with errors are excluded from dehydration by default.
127
127
- Serialize and embed `dehydratedState` in the markup
128
128
- Security note: Serializing data with `JSON.stringify` can put you at risk for XSS-vulnerabilities, [this blog post explains why and how to solve it](https://medium.com/node-security/the-most-common-xss-vulnerability-in-react-js-applications-2bdffbcc1fa0)
129
129
@@ -180,7 +180,7 @@ ReactDOM.hydrate(
180
180
181
181
Any query with an error is automatically excluded from dehydration. This means that the default behaviour is to pretend these queries were never loaded on the server, usually showing a loading state instead, and retrying the queries on the client. This happens regardless of error.
182
182
183
-
Sometimes this behavior is not desirable, maybe you want to render an error page with a correct status code instead on certain errors or queries. In those cases, pass `throwOnError: true` to the specific `prefetchQuery` to be able to catch and handle those errors manually.
183
+
Sometimes this behavior is not desirable, maybe you want to render an error page with a correct status code instead on certain errors or queries. In those cases, use `fetchQuery` and catch any errors to handle those manually.
184
184
185
185
**Staleness is measured from when the query was fetched on the server**
0 commit comments