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
- A function to clean the mutation internal state (i.e., it resets the mutation to its initial state).
346
346
347
-
## `queryCache`
348
-
349
-
The `queryCache` instance is the backbone of React Query that manages all of the state, caching, lifecycle and magic of every query. It supports relatively unrestricted, but safe, access to manipulate query's as you need. Its available properties and methods are:
The `QueryCache` is the backbone of React Query that manages all of the state, caching, lifecycle and magic of every query. It supports relatively unrestricted, but safe, access to manipulate query's as you need.
- Define defaults for all queries and mutations using this query cache.
377
382
378
383
## `queryCache.prefetchQuery`
379
384
@@ -426,8 +431,6 @@ The options for `prefetchQuery` are exactly the same as those of [`useQuery`](#u
426
431
`getQueryData` is a synchronous function that can be used to get an existing query's cached data. If the query does not exist, `undefined` will be returned.
427
432
428
433
```js
429
-
import { queryCache } from'react-query'
430
-
431
434
constdata=queryCache.getQueryData(queryKey)
432
435
```
433
436
@@ -448,8 +451,6 @@ const data = queryCache.getQueryData(queryKey)
448
451
> The difference between using `setQueryData` and `prefetchQuery` is that `setQueryData` is sync and assumes that you already synchronously have the data available. If you need to fetch the data asynchronously, it's suggested that you either refetch the query key or use `prefetchQuery` to handle the asynchronous fetch.
@@ -525,8 +524,6 @@ The `cancelQueries` method can be used to cancel outgoing queries based on their
525
524
This is most useful when performing optimistic updates since you will likely need to cancel any outgoing query refetches so they don't clobber your optimistic update when they resolve.
@@ -552,8 +549,6 @@ This function does not return anything
552
549
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.
@@ -581,8 +576,6 @@ This function does not return anything
581
576
> Note: This is not typically needed for most applications, but can come in handy when needing more information about a query in rare scenarios (eg. Looking at the query.state.updatedAt timestamp to decide whether a query is fresh enough to be used as an initial value)
This `isFetching` property is an `integer` representing how many queries, if any, in the cache are currently fetching (including background-fetching, loading new pages, or loading more infinite query results)
624
615
625
616
```js
626
-
import { queryCache } from'react-query'
627
-
628
617
if (queryCache.isFetching) {
629
618
console.log('At least one query is fetching!')
630
619
}
@@ -637,8 +626,6 @@ React Query also exports a handy [`useIsFetching`](#useisfetching) hook that wil
637
626
The `subscribe` method can be used to subscribe to the query cache as a whole and be informed of safe/known updates to the cache like query states changing or queries being updated, added or removed
The `clear` method can be used to clear the queryCache entirely and start fresh.
661
648
662
649
```js
663
-
import { queryCache } from'react-query'
664
-
665
650
queryCache.clear()
666
651
```
667
652
@@ -672,20 +657,7 @@ queryCache.clear()
672
657
673
658
## `makeQueryCache`
674
659
675
-
`makeQueryCache` creates an empty `queryCache` manually. This is useful together with `ReactQueryCacheProvider` to have multiple caches in your application.
676
-
677
-
As opposed to the global cache, caches created by `makeQueryCache` caches data even on the server.
678
-
679
-
```js
680
-
import { makeQueryCache } from'react-query'
681
-
682
-
constqueryCache=makeQueryCache()
683
-
```
684
-
685
-
**Returns**
686
-
687
-
- `queryCache: QueryCache`
688
-
- An empty `queryCache`
660
+
The `makeQueryCache` factory function has been deprecated in favor of `new QueryCache()`.
689
661
690
662
## `useQueryCache`
691
663
@@ -697,8 +669,6 @@ import { useQueryCache } from 'react-query'
697
669
constqueryCache=useQueryCache()
698
670
```
699
671
700
-
If you are using the `ReactQueryCacheProvider` to set a custom cache, you cannot simply import `{ queryCache }` any more. This hook will ensure you're getting the correct instance.
701
-
702
672
## `useIsFetching`
703
673
704
674
`useIsFetching` is an optional hook that returns the `number` of the queries that your application is loading or fetching in the background (useful for app-wide loading indicators).
@@ -772,12 +742,12 @@ function App() {
772
742
773
743
## `ReactQueryCacheProvider`
774
744
775
-
`ReactQueryCacheProvider` is an optional provider component for explicitly setting the query cache used by React Query. This is useful for creating component-level caches that are not completely global, as well as making truly isolated unit tests.
745
+
The query cache can be connected to React with the `ReactQueryCacheProvider`. This component puts the cache on the context, which enables you to access it from anywhere in your component tree.
When a successful `postTodo` mutation happens, we likely want all `todos` queries to get invalidated and possibly refetched to show the new todo item. To do this, you can use `useMutation`'s `onSuccess` options and the `queryCache`'s `invalidateQueries` function:
Copy file name to clipboardExpand all lines: docs/src/pages/docs/guides/prefetching.md
-4Lines changed: 0 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -6,8 +6,6 @@ title: Prefetching
6
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:
@@ -25,7 +23,5 @@ If a prefetched query is rendered after the `staleTime` for a prefetched query,
25
23
Alternatively, if you already have the data for your query synchronously available, you don't need to prefetch it. You can just use the [Query Cache's `setQueryData` method](../api/#querycachesetquerydata) to directly add or update a query's cached result.
Copy file name to clipboardExpand all lines: docs/src/pages/docs/guides/query-invalidation.md
+4-3Lines changed: 4 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -6,8 +6,6 @@ title: Query Invalidation
6
6
Waiting for queries to become stale before they are fetched again doesn't always work, especially when you know for a fact that a query needs to get refetched. For that purpose, the `queryCache` has an `invalidateQueries` method that lets you manually mark queries as stale and potentially refetch them too!
7
7
8
8
```js
9
-
import { queryCache } from'react-query'
10
-
11
9
queryCache.invalidateQueries('todos')
12
10
```
13
11
@@ -25,7 +23,10 @@ When using APIs like `invalidateQueries` and `removeQueries` (and others that su
25
23
In this example, we can use the `todos` prefix to invalidate any queries that start with `todos` in their query key:
@@ -77,11 +79,11 @@ Now you are ready to prefetch some data in your pages with either [`getStaticPro
77
79
78
80
```jsx
79
81
// pages/posts.jsx
80
-
import { makeQueryCache } from'react-query'
82
+
import { QueryCache } from'react-query'
81
83
import { dehydrate } from'react-query/hydration'
82
84
83
85
exportasyncfunctiongetStaticProps() {
84
-
constqueryCache=makeQueryCache()
86
+
constqueryCache=newQueryCache()
85
87
86
88
awaitqueryCache.prefetchQuery('posts', getPosts)
87
89
@@ -116,37 +118,38 @@ Since there are many different possible setups for SSR, it's hard to give a deta
116
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.
117
119
118
120
- Prefetch data
119
-
- Create a `prefetchQueryCache` specifically for prefetching by calling `const prefetchQueryCache = makeQueryCache()`
121
+
- Create a `prefetchQueryCache` specifically for prefetching by calling `const prefetchQueryCache = new QueryCache()`
120
122
- Call `prefetchQueryCache.prefetchQuery(...)` to prefetch queries
121
123
- Dehydrate by using `const dehydratedState = dehydrate(prefetchQueryCache)`
122
124
- Render
123
-
- Wrap the app in `<ReactQueryCacheProvider>` and `<Hydrate>` to create a new cache and hydrate the state
124
-
- This makes sure a separate `queryCache` is created specifically for rendering
125
-
-**Do not** pass in the `prefetchQueryCache` from the last step, 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 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.
126
127
- Serialize and embed `dehydratedState` in the markup
127
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)
128
129
129
130
**Client side**
130
131
131
132
- Parse `dehydratedState` from where you put it in the markup
133
+
- Create a cache and hydrate the state
132
134
- Render
133
-
- Wrap the app in `<Hydrate>` from `'react-query/hydration'` and pass in the dehyrated state.
134
135
135
136
This list aims to be exhaustive, but depending on your current setup, the above steps can take more or less work. Here is a barebones example:
0 commit comments