Skip to content

Commit

Permalink
docs: Update dependent-queries.md (#5963)
Browse files Browse the repository at this point in the history
* Update dependent-queries.md

This is to show how the useQueries hook can also be dependent

* Update dependent-queries.md

* Update dependent-queries.md

* Update docs/react/guides/dependent-queries.md

Thanks

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>

* Update docs/react/guides/dependent-queries.md

This makes things easy, sometimes I forget we have the select option 馃槄

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>

* Update dependent-queries.md

---------

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
  • Loading branch information
jomefavourite and TkDodo committed Sep 8, 2023
1 parent 515803f commit 4d859e9
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions docs/react/guides/dependent-queries.md
Expand Up @@ -3,6 +3,8 @@ id: dependent-queries
title: Dependent Queries
---

## useQuery dependent Query

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:

[//]: # 'Example'
Expand Down Expand Up @@ -51,3 +53,34 @@ Once we have the projects, it will go to:
status: 'success'
fetchStatus: 'idle'
```

## useQueries dependent Query

Dynamic parallel query - `useQueries` can depend on a previous query also, here's how to achieve this:

[//]: # 'Example2'

```tsx
// Get the users ids
const { data: userIds } = useQuery({
queryKey: ['users'],
queryFn: getUsersData,
select: users => users.map(user => user.id),
})

// Then get the users messages
const usersMessages = useQueries({
queries: users
? usersId.map(id => {
return {
queryKey: ['messages', id],
queryFn: () => getMessagesByUsers(id),
};
})
: [], // if users is undefined, an empty array will be returned
})
```

[//]: # 'Example2'

**Note** that `useQueries` return an **array of query results**

0 comments on commit 4d859e9

Please sign in to comment.