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 React Query's `useQuery` and `useInfiniteQuery` hooks side-by-side!
11β
12```tsx
13function App () {
14 // The following queries will execute in parallel
15 const usersQuery = useQuery(['users'], fetchUsers)
16 const teamsQuery = useQuery(['teams'], fetchTeams)
17 const projectsQuery = useQuery(['projects'], fetchProjects)
18 ...
19}
20```
21β
22> 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 `useQueries` hook (which is suggested) or orchestrate your own parallelism with separate components for each `useQuery` instance (which is lame).
23β
24## Dynamic Parallel Queries with `useQueries`
25β
26If 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.
27β
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**:
29β
30```tsx
31function App({ users }) {
32 const userQueries = useQueries({
33 queries: users.map(user => {
34 return {
35 queryKey: ['user', user.id],
36 queryFn: () => fetchUserById(user.id),
37 }
38 })
39 })
40}
41```