Open History to see the last commit
1---
2id: parallel-queries
3title: Parallel Queries
4---
5β
6"Parallel" queries are queries that are executed in parallel, or at the same time so as to maximize fetching concurrency.
7β
8## Manual Parallel Queries
9β
10When the number of parallel queries does not change, there is **no extra effort** to use parallel queries. Just use any number of TanStack Query's `useQuery` and `useInfiniteQuery` hooks side-by-side!
11β
12[//]: # 'Example'
13β
14```tsx
15function App () {
16 // The following queries will execute in parallel
17 const usersQuery = useQuery({ queryKey: ['users'], queryFn: fetchUsers })
18 const teamsQuery = useQuery({ queryKey: ['teams'], queryFn: fetchTeams })
19 const projectsQuery = useQuery({ queryKey: ['projects'], queryFn: fetchProjects })
20 ...
21}
22```
23β
24[//]: # 'Example'
25[//]: # 'Info'
26β
27> When using React Query in suspense mode, this pattern of parallelism does not work, since the first query would throw a promise internally and would suspend the component before the other queries run. To get around this, you'll either need to use the `useSuspenseQueries` hook (which is suggested) or orchestrate your own parallelism with separate components for each `useSuspenseQuery` instance.
28β
29[//]: # 'Info'
30β
31## Dynamic Parallel Queries with `useQueries`
32β
33[//]: # 'DynamicParallelIntro'
34β
35If 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, TanStack Query provides a `useQueries` hook, which you can use to dynamically execute as many queries in parallel as you'd like.
36β
37[//]: # 'DynamicParallelIntro'
38[//]: # 'DynamicParallelDescription'
39β
40`useQueries` accepts an **options object** with a **queries key** whose value is an **array of query objects**. It returns an **array of query results**:
41β
42[//]: # 'DynamicParallelDescription'
43[//]: # 'Example2'
44β
45```tsx
46function App({ users }) {
47 const userQueries = useQueries({
48 queries: users.map((user) => {
49 return {
50 queryKey: ['user', user.id],
51 queryFn: () => fetchUserById(user.id),
52 }
53 }),
54 })
55}
56```
57β
58[//]: # 'Example2'
59[//]: # 'TypeScriptSelect'
60β
61> When using TypeScript, an inline `select` written on a query object passed to `useQueries` can't infer its `data` argument from that same object's `queryFn` β it falls back to `unknown`. Annotate the `select` parameter explicitly, or define the query with the [`queryOptions`](../reference/functions/queryOptions.md) helper, to keep type inference. See [this known limitation](https://github.com/TanStack/query/issues/6556).
62β
63[//]: # 'TypeScriptSelect'