Skip to content

Commit 1bd0557

Browse files
feat: add more information about parallel queries
1 parent 10f2a50 commit 1bd0557

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

frontend/react/style-guide.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,10 @@ useQuery(['todos', todoId], async () => {
10941094
10951095
### Parallel Queries
10961096
Use to execute queries at the same time so as to maximize fetching concurrency.
1097+
1098+
**Manual Parallel Queries**
1099+
1100+
Use when the number of parallel queries does not change. Just use any number of `useQuery` or `useInfiniteQuery` hook side by side.
10971101
```tsx
10981102
function App () {
10991103
// The following queries will execute in parallel due to number of parallel
@@ -1103,12 +1107,19 @@ function App () {
11031107
const projectsQuery = useQuery(['projects'], fetchProjects)
11041108
...
11051109
}
1110+
```
1111+
>When using React Query in [suspense mode](https://tanstack.com/query/v4/docs/guides/suspense), this pattern of parallelism does not work due to the first query would suspend the component before the other queries run. To avoid this behavior, use `useQueries`.
11061112
1113+
**Dynamic Parallel Queries**
1114+
1115+
Use when the number of queries you need to execute is changing from render to render.
1116+
>You cannot use manual querying since that would violate the rules of hooks.
1117+
```tsx
1118+
// ❌ Bad
11071119
function App () {
1108-
// ❌ Bad
1109-
const userQueries = users.map(user => {
1110-
return useQuery(['user', user.id], () => fetchUserById(user.id))
1111-
})
1120+
const userQueries = users.map(user => {
1121+
return useQuery(['user', user.id], () => fetchUserBy(user.id))
1122+
})
11121123
}
11131124
11141125
// ✅ Good

0 commit comments

Comments
 (0)