close
Skip to content

Update dependent-queries.md - #5963

Merged
TkDodo merged 6 commits into
TanStack:mainfrom
jomefavourite:patch-1
Sep 8, 2023
Merged

Update dependent-queries.md#5963
TkDodo merged 6 commits into
TanStack:mainfrom
jomefavourite:patch-1

Conversation

@jomefavourite

@jomefavourite jomefavourite commented Sep 5, 2023

Copy link
Copy Markdown
Contributor

This is to show how the useQueries hook can also be dependent on a query.


useQueries dependent Query

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

// Get the users Data
const { data: users } = useQuery({
  queryKey: ['users'],
  queryFn: getUsersData,
})

const usersId = users?.filter(user => user?.id)

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

Note that useQueries return an array of query results

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

vercel Bot commented Sep 5, 2023

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
query ⬜️ Ignored (Inspect) Visit Preview Sep 6, 2023 0:29am

@codesandbox-ci

codesandbox-ci Bot commented Sep 5, 2023

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 28a94e2:

Sandbox Source
@tanstack/query-example-react-basic-typescript Configuration
@tanstack/query-example-solid-basic-typescript Configuration
@tanstack/query-example-svelte-basic Configuration
@tanstack/query-example-vue-basic Configuration

Comment thread docs/react/guides/dependent-queries.md Outdated
Comment thread docs/react/guides/dependent-queries.md Outdated
Comment thread docs/react/guides/dependent-queries.md Outdated
Comment thread docs/react/guides/dependent-queries.md
jomefavourite and others added 3 commits September 6, 2023 11:29
Thanks

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
This makes things easy, sometimes I forget we have the select option 😅

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
@jomefavourite
jomefavourite requested a review from TkDodo September 7, 2023 23:29
@TkDodo
TkDodo merged commit 4d859e9 into TanStack:main Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants