Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

dependent-queries.md

44 lines930 Bytes

1---
2id: dependent-queries
3title: Dependent Queries
4---
5​
6Dependent (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​
8```tsx
9// Get the user
10const { data: user } = useQuery(['user', email], getUserByEmail)
11​
12const userId = user?.id
13​
14// Then get the user's projects
15const { status, fetchStatus, data: projects } = useQuery(
16 ['projects', userId],
17 getProjectsByUser,
18 {
19 // The query will not execute until the userId exists
20 enabled: !!userId,
21 }
23```
24​
25The `projects` query will start in:
26​
27```tsx
28status: 'loading'
29fetchStatus: 'idle'
30```
31​
32As soon as the `user` is available, the `projects` query will be `enabled` and will then transition to:
33​
34```tsx
35status: 'loading'
36fetchStatus: 'fetching'
37```
38​
39Once we have the projects, it will go to:
40​
41```tsx
42status: 'success'
43fetchStatus: 'idle'
44```