Skip to content

Commit 5bee9b6

Browse files
committed
feat: api update
1 parent b65c0a0 commit 5bee9b6

File tree

31 files changed

+116
-1210
lines changed

31 files changed

+116
-1210
lines changed

docs/src/pages/docs/api.md

Lines changed: 0 additions & 1001 deletions
This file was deleted.

docs/src/pages/guides/default-query-function.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ const defaultQueryFn = async key => {
1313
}
1414

1515
// provide the default query function to your app with defaultOptions
16-
const queryCache = new QueryCache()
1716
const queryClient = new QueryClient({
18-
queryCache,
1917
defaultOptions: {
2018
queries: {
2119
queryFn: defaultQueryFn,

docs/src/pages/guides/migrating-to-react-query-3.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,9 @@ This has some benefits:
2323
Use the `QueryClientProvider` component to connect a `QueryClient` to your application:
2424

2525
```js
26-
import { QueryClient, QueryClientProvider, QueryCache } from 'react-query'
26+
import { QueryClient, QueryClientProvider } from 'react-query'
2727

28-
// Create query cache
29-
const queryCache = new QueryCache()
30-
31-
// Create mutation cache (can be omitted to reduce file size when not using mutations)
32-
const mutationCache = new MutationCache()
33-
34-
// Create client
35-
const queryClient = new QueryClient({ queryCache, mutationCache })
28+
const queryClient = new QueryClient()
3629

3730
function App() {
3831
return <QueryClientProvider client={queryClient}>...</QueryClientProvider>
@@ -64,7 +57,6 @@ The `ReactQueryConfigProvider` component has been removed. Default options for q
6457

6558
```js
6659
const queryClient = new QueryClient({
67-
queryCache,
6860
defaultOptions: {
6961
queries: {
7062
staleTime: Infinity,
@@ -225,15 +217,15 @@ useQuery({
225217
226218
The `queryClient.prefetchQuery()` method should now only be used for prefetching scenarios where the result is not relevant.
227219
228-
Use the `queryClient.fetchQueryData()` method to get the query data or error:
220+
Use the `queryClient.fetchQuery()` method to get the query data or error:
229221
230222
```js
231223
// Prefetch a query:
232224
await queryClient.prefetchQuery('posts', fetchPosts)
233225

234226
// Fetch a query:
235227
try {
236-
const data = await queryClient.fetchQueryData('posts', fetchPosts)
228+
const data = await queryClient.fetchQuery('posts', fetchPosts)
237229
} catch (error) {
238230
// Error handling
239231
}

docs/src/pages/guides/query-retries.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ The default `retryDelay` is set to double (starting at `1000`ms) with each attem
3131
// Configure for all queries
3232
import { QueryCache, QueryClient, QueryClientProvider } from 'react-query'
3333

34-
const queryCache = new QueryCache()
3534
const queryClient = new QueryClient({
36-
queryCache,
3735
defaultOptions: {
3836
queries: {
3937
retryDelay: attemptIndex => Math.min(1000 * 2 ** attemptIndex, 30000),

docs/src/pages/guides/ssr.md

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,16 @@ React Query supports prefetching multiple queries on the server in Next.js and t
4949

5050
To support caching queries on the server and set up hydration:
5151

52-
- Create a new `QueryCache` and `QueryClient` instance
52+
- Create a new `QueryClient` instance
5353
- Wrap your app component with `<QueryClientProvider>` and pass it the client instance
5454
- Wrapp your app component with `<Hydrate>` and pass it the `dehydratedState` prop from `pageProps`
5555

5656
```js
5757
// _app.jsx
58-
import { QueryCache, QueryClient, QueryClientProvider } from 'react-query'
58+
import { QueryClient, QueryClientProvider } from 'react-query'
5959
import { Hydrate } from 'react-query/hydration'
6060

61-
const queryCache = new QueryCache()
62-
const queryClient = new QueryClient({ queryCache })
61+
const queryClient = new QueryClient()
6362

6463
export default function MyApp({ Component, pageProps }) {
6564
return (
@@ -74,18 +73,17 @@ export default function MyApp({ Component, pageProps }) {
7473

7574
Now you are ready to prefetch some data in your pages with either [`getStaticProps`](https://nextjs.org/docs/basic-features/data-fetching#getstaticprops-static-generation) (for SSG) or [`getServerSideProps`](https://nextjs.org/docs/basic-features/data-fetching#getserversideprops-server-side-rendering) (for SSR). From React Query's perspective, these integrate in the same way, `getStaticProps` is shown below.
7675

77-
- Create a new `QueryCache` and `QueryClient` instance for each page request
76+
- Create a new `QueryClient` instance for each page request
7877
- Prefetch the data using the clients `prefetchQuery` method and wait for it to complete
7978
- Use `dehydrate` to dehydrate the query cache and pass it to the page via the `dehydratedState` prop. This is the same prop that the cache will be picked up from in your `_app.js`
8079

8180
```js
8281
// pages/posts.jsx
83-
import { QueryCache, QueryClient, useQuery } from 'react-query'
82+
import { QueryClient, useQuery } from 'react-query'
8483
import { dehydrate } from 'react-query/hydration'
8584

8685
export async function getStaticProps() {
87-
const queryCache = new QueryCache()
88-
const queryClient = new QueryClient({ queryCache })
86+
const queryClient = new QueryClient()
8987

9088
await queryClient.prefetchQuery('posts', getPosts)
9189

@@ -128,11 +126,10 @@ This guide is at-best, a high level overview of how SSR with React Query should
128126
> 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)
129127
130128
```js
131-
import { QueryCache, QueryClient, QueryClientProvider } from 'react-query'
129+
import { QueryClient, QueryClientProvider } from 'react-query'
132130
import { dehydrate, Hydrate } from 'react-query/hydration'
133131

134-
const queryCache = new QueryCache()
135-
const queryClient = new QueryClient({ queryCache })
132+
const queryClient = new QueryClient()
136133
await queryClient.prefetchQuery('key', fn)
137134
const dehydratedState = dehydrate(client)
138135

@@ -159,18 +156,16 @@ res.send(`
159156
### Client
160157

161158
- Parse the dehydrated cache state that was sent to the client with the HTML
162-
- Create a new `QueryCache` instance
163159
- Create a new `QueryClient` instance
164160
- Render your app with the client provider and also **using the dehydrated state. This is extremely important! You must render both server and client using the same dehydrated state to ensure hydration on the client produces the exact same markup as the server.**
165161

166162
```js
167-
import { QueryCache, QueryClient, QueryClientProvider } from 'react-query'
163+
import { QueryClient, QueryClientProvider } from 'react-query'
168164
import { Hydrate } from 'react-query/hydration'
169165

170166
const dehydratedState = JSON.parse(window.__REACT_QUERY_STATE__)
171167

172-
const queryCache = new QueryCache()
173-
const queryClient = new QueryClient({ queryCache })
168+
const queryClient = new QueryClient()
174169

175170
ReactDOM.hydrate(
176171
<ReactQueryClientProvider client={queryClient}>
@@ -187,7 +182,7 @@ ReactDOM.hydrate(
187182

188183
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 queryClient. This happens regardless of error.
189184

190-
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 `fetchQueryData` and catch any errors to handle those manually.
185+
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.
191186

192187
### Staleness is measured from when the query was fetched on the server
193188

docs/src/pages/guides/suspense.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ Global configuration:
1111

1212
```js
1313
// Configure for all queries
14-
import { QueryCache, QueryClient, QueryClientProvider } from 'react-query'
14+
import { QueryClient, QueryClientProvider } from 'react-query'
1515

16-
const queryCache = new QueryCache()
1716
const queryClient = new QueryClient({
18-
queryCache,
1917
defaultOptions: {
2018
queries: {
2119
suspense: true,

docs/src/pages/overview.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,9 @@ In the example below, you can see React Query in its most basic and simple form
4747
[Open in CodeSandbox](https://codesandbox.io/s/github/tannerlinsley/react-query/tree/master/examples/simple)
4848

4949
```js
50-
import {
51-
useQuery,
52-
QueryCache,
53-
QueryClient,
54-
QueryClientProvider,
55-
} from 'react-query'
56-
57-
const queryCache = new QueryCache()
58-
const queryClient = new QueryClient({ queryCache })
50+
import { useQuery, QueryClient, QueryClientProvider } from 'react-query'
51+
52+
const queryClient = new QueryClient()
5953

6054
export default function App() {
6155
return (

docs/src/pages/quick-start.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,13 @@ import {
1414
useQuery,
1515
useMutation,
1616
useQueryClient,
17-
QueryCache,
1817
QueryClient,
1918
QueryClientProvider,
2019
} from 'react-query'
2120
import { getTodos, postTodo } from '../my-api'
2221

23-
// Create a cache
24-
const queryCache = new QueryCache()
25-
2622
// Create a client
27-
const queryClient = new QueryClient({ queryCache })
23+
const queryClient = new QueryClient()
2824

2925
function App() {
3026
return (

docs/src/pages/reference/QueryClient.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ The `QueryClient` can be used to interact with a cache:
1010
```js
1111
import { QueryClient, QueryCache } from 'react-query'
1212

13-
const queryCache = new QueryCache()
1413
const queryClient = new QueryClient({
15-
queryCache,
1614
defaultOptions: {
1715
queries: {
1816
staleTime: Infinity,
@@ -25,7 +23,7 @@ await queryClient.prefetchQuery('posts', fetchPosts)
2523

2624
Its available methods are:
2725

28-
- [`fetchQueryData`](#queryclientfetchquerydata)
26+
- [`fetchQuery`](#queryclientfetchquerydata)
2927
- [`prefetchQuery`](#queryclientprefetchquery)
3028
- [`getQueryData`](#queryclientgetquerydata)
3129
- [`setQueryData`](#queryclientsetquerydata)
@@ -49,17 +47,17 @@ Its available methods are:
4947
- Optional
5048
- Define defaults for all queries and mutations using this queryClient.
5149

52-
## `queryClient.fetchQueryData`
50+
## `queryClient.fetchQuery`
5351

54-
`fetchQueryData` 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. Use the `prefetchQuery` method if you just want to fetch a query without needing the result.
52+
`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. Use the `prefetchQuery` method if you just want to fetch a query without needing the result.
5553

5654
If the query exists and the data is not invalidated or older than the given `staleTime`, then the data from the cache will be returned. Otherwise it will try to fetch the latest data.
5755

58-
> The difference between using `fetchQueryData` and `setQueryData` is that `fetchQueryData` 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.
56+
> The difference between using `fetchQuery` and `setQueryData` is that `fetchQuery` 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.
5957
6058
```js
6159
try {
62-
const data = await queryClient.fetchQueryData(queryKey, queryFn)
60+
const data = await queryClient.fetchQuery(queryKey, queryFn)
6361
} catch (error) {
6462
console.log(error)
6563
}
@@ -69,7 +67,7 @@ Specify a `staleTime` to only fetch when the data is older than a certain amount
6967

7068
```js
7169
try {
72-
const data = await queryClient.fetchQueryData(queryKey, queryFn, {
70+
const data = await queryClient.fetchQuery(queryKey, queryFn, {
7371
staleTime: 10000,
7472
})
7573
} catch (error) {
@@ -79,15 +77,15 @@ try {
7977

8078
**Options**
8179

82-
The options for `fetchQueryData` are exactly the same as those of [`useQuery`](#usequery).
80+
The options for `fetchQuery` are exactly the same as those of [`useQuery`](#usequery).
8381

8482
**Returns**
8583

8684
- `Promise<TData>`
8785

8886
## `queryClient.prefetchQuery`
8987

90-
`prefetchQuery` is an asynchronous method that can be used to prefetch a query before it is needed or rendered with `useQuery` and friends. The method works the same as `fetchQueryData` except that is will not throw or return any data.
88+
`prefetchQuery` is an asynchronous method that can be used to prefetch a query before it is needed or rendered with `useQuery` and friends. The method works the same as `fetchQuery` except that is will not throw or return any data.
9189

9290
```js
9391
await queryClient.prefetchQuery(queryKey, queryFn)
@@ -130,7 +128,7 @@ const data = queryClient.getQueryData(queryKey)
130128

131129
`setQueryData` is a synchronous function that can be used to immediately update a query's cached data. If the query does not exist, it will be created. **If the query is not utilized by a query hook in the default `cacheTime` of 5 minutes, the query will be garbage collected**.
132130

133-
> The difference between using `setQueryData` and `fetchQueryData` 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 `fetchQueryData` to handle the asynchronous fetch.
131+
> The difference between using `setQueryData` and `fetchQuery` 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 `fetchQuery` to handle the asynchronous fetch.
134132
135133
```js
136134
queryClient.setQueryData(queryKey, updater)

docs/src/pages/reference/QueryClientProvider.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ title: QueryClientProvider
66
Use the `QueryClientProvider` component to connect and provide a `QueryClient` to your application:
77

88
```js
9-
import { QueryClient, QueryClientProvider, QueryCache } from 'react-query'
9+
import { QueryClient, QueryClientProvider } from 'react-query'
1010

11-
const queryCache = new QueryCache()
12-
const queryClient = new QueryClient({ queryCache })
11+
const queryClient = new QueryClient()
1312

1413
function App() {
1514
return <QueryClientProvider client={queryClient}>...</QueryClientProvider>

0 commit comments

Comments
 (0)