Skip to content

Commit af8a207

Browse files
committed
feat: initial v3 changes
1 parent e200c99 commit af8a207

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+3323
-3592
lines changed

docs/src/pages/docs/api.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const {
5252
const queryInfo = useQuery({
5353
queryKey,
5454
queryFn,
55-
config,
55+
enabled,
5656
})
5757
```
5858
@@ -124,6 +124,9 @@ const queryInfo = useQuery({
124124
- `onSettled: Function(data, error) => data`
125125
- Optional
126126
- This function will fire any time the query is either successfully fetched or errors and be passed either the data or error
127+
- `select: (data) => data`
128+
- Optional
129+
- This option can be used to transform or select a part of the data returned by the query function.
127130
- `suspense: Boolean`
128131
- Optional
129132
- Set this to `true` to enable suspense mode.
@@ -186,7 +189,7 @@ const queryInfo = useQuery({
186189
- The failure count for the query.
187190
- Incremented every time the query fails.
188191
- Reset to `0` when the query succeeds.
189-
- `refetch: Function({ throwOnError }) => Promise<TResult | undefined>`
192+
- `refetch: Function({ throwOnError }) => Promise<TData | undefined>`
190193
- A function to manually refetch the query.
191194
- If the query errors, the error will only be logged. If you want an error to be thrown, pass the `throwOnError: true` option
192195
- `remove: Function() => void`
@@ -253,7 +256,7 @@ The returned properties for `useInfiniteQuery` are identical to the [`useQuery`
253256
254257
- `isFetchingMore: false | 'next' | 'previous'`
255258
- If using `paginated` mode, this will be `true` when fetching more results using the `fetchMore` function.
256-
- `fetchMore: Function(fetchMoreVariableOverride) => Promise<TResult | undefined>`
259+
- `fetchMore: Function(fetchMoreVariableOverride) => Promise<TData | undefined>`
257260
- This function allows you to fetch the next "page" of results.
258261
- `fetchMoreVariableOverride` allows you to optionally override the fetch more variable returned from your `getFetchMore` option to your query function to retrieve the next page of results.
259262
- `canFetchMore: Boolean`
@@ -391,7 +394,7 @@ try {
391394
392395
**Returns**
393396
394-
- `Promise<TResult>`
397+
- `Promise<TData>`
395398
396399
## `queryCache.prefetchQuery`
397400
@@ -436,7 +439,7 @@ The options for `prefetchQuery` are exactly the same as those of [`useQuery`](#u
436439
437440
**Returns**
438441
439-
- `Promise<TResult | undefined>`
442+
- `Promise<TData | undefined>`
440443
- 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`.
441444
442445
## `queryCache.getQueryData`
@@ -837,16 +840,16 @@ function App() {
837840
- `queryCache: QueryCache`
838841
- Instance of QueryCache.
839842

840-
## `ReactQueryErrorResetBoundary`
843+
## `QueryErrorResetBoundary`
841844

842-
When using **suspense** or **useErrorBoundaries** in your queries, you need a way to let queries know that you want to try again when re-rendering after some error occured. With the `ReactQueryErrorResetBoundary` component you can reset any query errors within the boundaries of the component.
845+
When using **suspense** or **useErrorBoundaries** in your queries, you need a way to let queries know that you want to try again when re-rendering after some error occured. With the `QueryErrorResetBoundary` component you can reset any query errors within the boundaries of the component.
843846

844847
```js
845-
import { ReactQueryErrorResetBoundary } from 'react-query'
848+
import { QueryErrorResetBoundary } from 'react-query'
846849
import { ErrorBoundary } from 'react-error-boundary'
847850

848851
const App: React.FC = () => (
849-
<ReactQueryErrorResetBoundary>
852+
<QueryErrorResetBoundary>
850853
{({ reset }) => (
851854
<ErrorBoundary
852855
onReset={reset}
@@ -860,20 +863,20 @@ const App: React.FC = () => (
860863
<Page />
861864
</ErrorBoundary>
862865
)}
863-
</ReactQueryErrorResetBoundary>
866+
</QueryErrorResetBoundary>
864867
)
865868
```
866869

867-
## `useErrorResetBoundary`
870+
## `useQueryErrorResetBoundary`
868871

869-
This hook will reset any query errors within the closest `ReactQueryErrorResetBoundary`. If there is no boundary defined it will reset them globally:
872+
This hook will reset any query errors within the closest `QueryErrorResetBoundary`. If there is no boundary defined it will reset them globally:
870873

871874
```js
872-
import { useErrorResetBoundary } from 'react-query'
875+
import { useQueryErrorResetBoundary } from 'react-query'
873876
import { ErrorBoundary } from 'react-error-boundary'
874877

875878
const App: React.FC = () => {
876-
const { reset } = useErrorResetBoundary()
879+
const { reset } = useQueryErrorResetBoundary()
877880
return (
878881
<ErrorBoundary
879882
onReset={reset}

docs/src/pages/docs/comparison.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Feature/Capability Key:
2828
| Paginated Queries ||||
2929
| Infinite Queries ||||
3030
| Lagged / "Lazy" Queries<sup>1</sup> || 🛑 | 🛑 |
31+
| Selectors || 🛑 ||
3132
| Initial Data ||||
3233
| Scroll Recovery ||||
3334
| Cache Manipulation ||||
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
---
2+
id: migrating-to-react-query-3
3+
title: Migrating to React Query 3
4+
---
5+
6+
## V3 migration
7+
8+
This article explains how to migrate your application to React Query 3.
9+
10+
### QueryClient
11+
12+
The `QueryCache` has been split into a `QueryClient` and a `QueryCache`.
13+
The `QueryCache` contains all cached queries and the `QueryClient` can be used to interact with a cache.
14+
15+
This has some benefits:
16+
17+
- Allows for different type of caches.
18+
- Multiple clients with different configurations can use the same cache.
19+
- Clients can be used to track queries, which can be used for shared caches on SSR.
20+
- The client API is more focused towards general usage.
21+
- Easier to test the individual components.
22+
23+
Use the `QueryClientProvider` component to connect a `QueryClient` to your application:
24+
25+
```js
26+
import { QueryClient, QueryClientProvider, QueryCache } from 'react-query'
27+
28+
const cache = new QueryCache()
29+
const client = new QueryClient({ cache })
30+
31+
function App() {
32+
return <QueryClientProvider client={client}>...</QueryClientProvider>
33+
}
34+
```
35+
36+
### useQueryCache()
37+
38+
The `useQueryCache()` hook has been replaced by the `useQueryClient()` hook:
39+
40+
```js
41+
import { useCallback } from 'react'
42+
import { useQueryClient } from 'react-query'
43+
44+
function Todo() {
45+
const client = useQueryClient()
46+
47+
const onClickButton = useCallback(() => {
48+
client.refetchQueries('posts')
49+
}, [client])
50+
51+
return <button onClick={onClickButton}>Refetch</button>
52+
}
53+
```
54+
55+
### ReactQueryConfigProvider
56+
57+
The `ReactQueryConfigProvider` component has been removed. Default options for queries and mutations can now be specified in `QueryClient`:
58+
59+
```js
60+
const client = new QueryClient({
61+
cache,
62+
defaultOptions: {
63+
queries: {
64+
staleTime: Infinity,
65+
},
66+
},
67+
})
68+
```
69+
70+
### usePaginatedQuery()
71+
72+
The `usePaginatedQuery()` hook has been replaced by the `keepPreviousData` option on `useQuery`:
73+
74+
```js
75+
import { useQuery } from 'react-query'
76+
77+
function Page({ page }) {
78+
const { data } = useQuery(['page', page], fetchPage, {
79+
keepPreviousData: true,
80+
})
81+
}
82+
```
83+
84+
### Query object syntax
85+
86+
The object syntax has been collapsed:
87+
88+
```js
89+
// Old:
90+
useQuery({
91+
queryKey: 'posts',
92+
queryFn: fetchPosts,
93+
config: { staleTime: Infinity },
94+
})
95+
96+
// New:
97+
useQuery({
98+
queryKey: 'posts',
99+
queryFn: fetchPosts,
100+
staleTime: Infinity,
101+
})
102+
```
103+
104+
### queryCache.invalidateQueries()
105+
106+
The `client.invalidateQueries()` method will now only invalidate queries.
107+
All matched queries will be marked invalid and refetched on window focus, reconnect or mounting of components.
108+
Use `client.refetchQueries()` to refetch queries immediately.
109+
110+
### queryCache.refetchQueries()
111+
112+
The `client.refetchQueries()` method can be used to refetch queries like the `refetch()` method.
113+
By default it will only refetch active queries, but an `inactive` flag can be set to also refetch inactive queries.
114+
115+
```js
116+
// Refetch all active queries:
117+
client.refetchQueries()
118+
119+
// Also refetch inactive queries:
120+
client.refetchQueries({ inactive: true })
121+
122+
// Fetch active queries matching a query key:
123+
client.refetchQueries('posts')
124+
125+
// Also fetch inactive queries:
126+
client.refetchQueries('posts', { inactive: true })
127+
128+
// Only fetch inactive queries:
129+
client.refetchQueries('posts', { active: false, inactive: true })
130+
```
131+
132+
The same filters can be used in the `client.cancelQueries()`, `client.invalidateQueries()` and `client.removeQueries()` methods but these methods will include all queries by default.
133+
134+
### queryCache.prefetchQuery()
135+
136+
The `client.prefetchQuery()` method should now only be used for prefetching scenarios where the result is not relevant.
137+
138+
Use the `client.fetchQueryData()` method to get the query data or error:
139+
140+
```js
141+
// Prefetch a query:
142+
await client.prefetchQuery('posts', fetchPosts)
143+
144+
// Fetch a query:
145+
try {
146+
const data = await client.fetchQueryData('posts', fetchPosts)
147+
} catch (error) {
148+
// Error handling
149+
}
150+
```
151+
152+
### ReactQueryCacheProvider
153+
154+
The `ReactQueryCacheProvider` component has been replaced by the `QueryClientProvider` component.
155+
156+
### makeQueryCache()
157+
158+
The `makeQueryCache()` function has replaced by `new QueryCache()`.
159+
160+
### ReactQueryErrorResetBoundary
161+
162+
The `ReactQueryErrorResetBoundary` component has been renamed to `QueryErrorResetBoundary`.
163+
164+
### queryCache.resetErrorBoundaries()
165+
166+
The `queryCache.resetErrorBoundaries()` method has been replaced by the `QueryErrorResetBoundary` component.
167+
168+
### queryCache.getQuery()
169+
170+
The `queryCache.getQuery()` method has been replaced by `cache.find()`.
171+
172+
### queryCache.getQueries()
173+
174+
The `queryCache.getQueries()` method has been replaced by `cache.findAll()`.
175+
176+
### queryCache.isFetching
177+
178+
The `queryCache.isFetching` property has been replaced by `client.isFetching()`.
179+
180+
### QueryOptions.initialStale
181+
182+
The `initialStale` query option has been removed and initial data is now treated as regular data.
183+
Which means that if `initialData` is provided, the query will refetch on mount by default.
184+
If you do not want to refetch immediately, you can define a `staleTime`.
185+
186+
### QueryOptions.forceFetchOnMount
187+
188+
The `forceFetchOnMount` query option has been replaced by `refetchOnMount: 'always'`.
189+
190+
### QueryOptions.refetchOnMount
191+
192+
When `refetchOnMount` was set to `false` any additional components were prevented from refetching on mount.
193+
In version 3 only the component where the option has been set will not refetch on mount.
194+
195+
### QueryResult.clear()
196+
197+
The `QueryResult.clear()` method has been renamed to `QueryResult.remove()`.
198+
199+
### New features
200+
201+
Some new features have also been added besides the API changes, performance improvements and file size reduction.
202+
203+
#### Selectors
204+
205+
The `useQuery` and `useInfiniteQuery` hooks now have a `select` option to select or transform parts of the query result.
206+
207+
```js
208+
import { useQuery } from 'react-query'
209+
210+
function User() {
211+
const { data } = useQuery('user', fetchUser, {
212+
select: user => user.username,
213+
})
214+
return <div>Username: {data}</div>
215+
}
216+
```
217+
218+
Set the `notifyOnStatusChange` option to `false` to only re-render when the selected data changes.
219+
220+
#### client.watchQuery()
221+
222+
The `client.watchQuery()` method can be used to create and/or watch a query:
223+
224+
```js
225+
const observer = client.watchQuery('posts)
226+
227+
observer.subscribe(result => {
228+
console.log(result)
229+
observer.unsubscribe()
230+
})
231+
```
232+
233+
#### React Native error screens
234+
235+
To prevent showing error screens in React Native when a query fails it was necessary to manually change the Console:
236+
237+
```js
238+
import { setConsole } from 'react-query'
239+
240+
setConsole({
241+
log: console.log,
242+
warn: console.warn,
243+
error: console.warn,
244+
})
245+
```
246+
247+
In version 3 this is done automatically when React Query is used in React Native.

0 commit comments

Comments
 (0)