Skip to content

Commit 942805f

Browse files
feat(useQueries): v4 api to take an object as input (#3071)
* feat(useQueries): update API to use object syntax New v4 API - instead of taking an array of queries, `useQueries` now accepts an object with a `queries` key. The value of this key is an array of queries (this array is unchanged from v3). * test(useQueries): update tests for new API * docs(useQueries): update docs for v4 API
1 parent 1247d7b commit 942805f

File tree

5 files changed

+434
-384
lines changed

5 files changed

+434
-384
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ new QueryClient({
207207
})
208208
```
209209

210+
### new API for `useQueries`
211+
212+
The `useQueries` hook now accepts an object with a `queries` prop as its input. The value of the `queries` prop is an array of queries (this array is identical to what was passed into `useQueries` in v3).
213+
214+
```diff
215+
- useQueries([{ queryKey1, queryFn1, options1 }, { queryKey2, queryFn2, options2 }])
216+
+ useQueries({ queries: [{ queryKey1, queryFn1, options1 }, { queryKey2, queryFn2, options2 }] })
217+
```
218+
219+
210220
### Removed undocumented methods from the `queryClient`
211221

212222
The methods `cancelMutatations` and `executeMutation` were undocumented and unused internally, so we removed them. Since they were just wrappers around methods available on the `mutationCache`, you can still use the functionality.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ function App () {
2525

2626
If the number of queries you need to execute is changing from render to render, you cannot use manual querying since that would violate the rules of hooks. Instead, React Query provides a `useQueries` hook, which you can use to dynamically execute as many queries in parallel as you'd like.
2727

28-
`useQueries` accepts an **array of query options objects** and returns an **array of query results**:
28+
`useQueries` accepts an **options object** with a **queries key** whose value is an **array of query objects**. It returns an **array of query results**:
2929

3030
```js
3131
function App({ users }) {
32-
const userQueries = useQueries(
33-
users.map(user => {
32+
const userQueries = useQueries({
33+
queries: users.map(user => {
3434
return {
3535
queryKey: ['user', user.id],
3636
queryFn: () => fetchUserById(user.id),
3737
}
3838
})
39-
)
39+
})
4040
}
4141
```

docs/src/pages/reference/useQueries.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ title: useQueries
66
The `useQueries` hook can be used to fetch a variable number of queries:
77

88
```js
9-
const results = useQueries([
10-
{ queryKey: ['post', 1], queryFn: fetchPost },
11-
{ queryKey: ['post', 2], queryFn: fetchPost },
12-
])
9+
const results = useQueries({
10+
queries: [
11+
{ queryKey: ['post', 1], queryFn: fetchPost },
12+
{ queryKey: ['post', 2], queryFn: fetchPost }
13+
]
14+
})
1315
```
1416

1517
**Options**
1618

17-
The `useQueries` hook accepts an array with query option objects identical to the [`useQuery` hook](/reference/useQuery).
19+
The `useQueries` hook accepts an options object with a **queries** key whose value is an array with query option objects identical to the [`useQuery` hook](/reference/useQuery).
1820

1921
**Returns**
2022

0 commit comments

Comments
 (0)