Skip to content

Commit 613f09e

Browse files
committed
feat: initial v3 changes
1 parent e16f3dd commit 613f09e

Some content is hidden

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

69 files changed

+3885
-4056
lines changed

.github/workflows/test-and-publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
registry-url: https://registry.npmjs.org/
3939
- name: Install dependencies
4040
uses: bahmutov/npm-install@v1
41-
- run: yarn build && yarn build:types
41+
- run: yarn build
4242
- run: npx semantic-release@17
4343
env:
4444
NODE_AUTH_TOKEN: ${{secrets.npm_token}}

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
node_modules
66

77
# builds
8+
types
89
build
910
dist
1011
lib
1112
es
1213
artifacts
1314
.rpt2_cache
1415
coverage
16+
*.tgz
1517

1618
# misc
1719
.DS_Store
@@ -31,5 +33,3 @@ stats-hydration.json
3133
stats-react.json
3234
stats.html
3335
.vscode/settings.json
34-
35-
types

core/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"internal": true,
3+
"main": "../lib/core/index.js",
4+
"module": "../es/core/index.js",
5+
"types": "../types/core/index.d.ts"
6+
}

docs/src/pages/docs/api.md

Lines changed: 297 additions & 371 deletions
Large diffs are not rendered by default.

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: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
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+
// Refetch all active and inactive queries:
120+
client.refetchQueries({ active: true, inactive: true })
121+
122+
// Fetch active queries matching a query key:
123+
client.refetchQueries('posts')
124+
125+
// Refetch active and inactive queries:
126+
client.refetchQueries('posts', { active: true, 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+
#### useQueries()
221+
222+
The `useQueries()` hook can be used to fetch a variable number of queries:
223+
224+
```js
225+
import { useQueries } from 'react-query'
226+
227+
function Overview() {
228+
const results = useQueries([
229+
{ queryKey: ['post', 1], queryFn: fetchPost },
230+
{ queryKey: ['post', 2], queryFn: fetchPost },
231+
])
232+
return (
233+
<ul>
234+
{results.map(({ data }) => data && <li key={data.id}>{data.title})</li>)}
235+
</ul>
236+
)
237+
}
238+
```
239+
240+
#### client.watchQuery()
241+
242+
The `client.watchQuery()` method can be used to create and/or watch a query:
243+
244+
```js
245+
const observer = client.watchQuery('posts')
246+
247+
observer.subscribe(result => {
248+
console.log(result)
249+
observer.unsubscribe()
250+
})
251+
```
252+
253+
#### client.watchQueries()
254+
255+
The `client.watchQueries()` method can be used to create and/or watch multiple queries:
256+
257+
```js
258+
const observer = client.watchQueries([
259+
{ queryKey: ['post', 1], queryFn: fetchPost },
260+
{ queryKey: ['post', 2], queryFn: fetchPost },
261+
])
262+
263+
observer.subscribe(result => {
264+
console.log(result)
265+
observer.unsubscribe()
266+
})
267+
```
268+
269+
#### React Native error screens
270+
271+
To prevent showing error screens in React Native when a query fails it was necessary to manually change the Console:
272+
273+
```js
274+
import { setConsole } from 'react-query'
275+
276+
setConsole({
277+
log: console.log,
278+
warn: console.warn,
279+
error: console.warn,
280+
})
281+
```
282+
283+
In version 3 this is done automatically when React Query is used in React Native.
284+
285+
#### Core separation
286+
287+
The core of React Query is now fully separated from React, which means it can also be used standalone or in other frameworks. Use the `react-query/core` entrypoint to only import the core functionality:
288+
289+
```js
290+
import { QueryClient } from 'react-query/core'
291+
```

docs/src/pages/docs/guides/queries.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,33 @@ function GlobalLoadingIndicator() {
257257
) : null
258258
}
259259
```
260+
261+
# Query Filters
262+
263+
Some methods within React Query accept a `QueryFilters` object. A query filter is an object with certain conditions to match a query with:
264+
265+
```js
266+
await client.refetchQueries({ active: true, inactive: true })
267+
await client.refetchQueries('posts', { active: true, inactive: true })
268+
```
269+
270+
A query filter object supports the following properties:
271+
272+
- `exact?: boolean`
273+
- 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.
274+
- `active?: boolean`
275+
- When set to `true` it will match active queries.
276+
- When set to `false` it will match inactive queries.
277+
- `inactive?: boolean`
278+
- When set to `true` it will match active queries.
279+
- When set to `false` it will match inactive queries.
280+
- `stale?: boolean`
281+
- When set to `true` it will match stale queries.
282+
- When set to `false` it will not match stale queries.
283+
- `fresh?: boolean`
284+
- When set to `true` it will match fresh queries.
285+
- When set to `false` it will not match fresh queries.
286+
- `predicate?: (query: Query) => boolean`
287+
- This predicate function will be called for every single query in the cache and be expected to return truthy for queries that are `found`.
288+
- `queryKey?: QueryKey`
289+
- Set this property to define a query key to match on.

0 commit comments

Comments
 (0)