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
Copy file name to clipboardExpand all lines: docs/react/guides/dependent-queries.md
+33Lines changed: 33 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,8 @@ id: dependent-queries
3
3
title: Dependent Queries
4
4
---
5
5
6
+
## useQuery dependent Query
7
+
6
8
Dependent (or serial) queries depend on previous ones to finish before they can execute. To achieve this, it's as easy as using the `enabled` option to tell a query when it is ready to run:
7
9
8
10
[//]: #'Example'
@@ -54,3 +56,34 @@ status: 'success'
54
56
isPending: false
55
57
fetchStatus: 'idle'
56
58
```
59
+
60
+
## useQueries dependent Query
61
+
62
+
Dynamic parallel query - `useQueries` can depend on a previous query also, here's how to achieve this:
63
+
64
+
[//]: #'Example2'
65
+
66
+
```tsx
67
+
// Get the users ids
68
+
const { data: userIds } =useQuery({
69
+
queryKey: ['users'],
70
+
queryFn: getUsersData,
71
+
select: users=>users.map(user=>user.id),
72
+
})
73
+
74
+
// Then get the users messages
75
+
const usersMessages =useQueries({
76
+
queries: users
77
+
?usersId.map(id=> {
78
+
return {
79
+
queryKey: ['messages', id],
80
+
queryFn: () =>getMessagesByUsers(id),
81
+
};
82
+
})
83
+
: [], // if users is undefined, an empty array will be returned
84
+
})
85
+
```
86
+
87
+
[//]: #'Example2'
88
+
89
+
**Note** that `useQueries` return an **array of query results**
0 commit comments