How to handle dependant queries using react-query in next JS getServersideProps #4356
-
i need wait one query and then get variable from first query and add it to the second query |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You need to use, chain queries, take a look at: https://tanstack.com/query/v4/docs/guides/dependent-queries?from=reactQueryV3&original=https://react-query-v3.tanstack.com/guides/dependent-queries e.g. // YOUR FIRST QUERY
const { data: user } = useQuery(['user', email], getUserByEmail)
const userId = user?.id
// QUERY WHICH DEPENDS ON THE USER
const { status, fetchStatus, data: projects } = useQuery(
['projects', userId],
getProjectsByUser,
{
// The query will not execute until the userId exists
enabled: !!userId,
}
) |
Beta Was this translation helpful? Give feedback.
-
the SSR docs show how to use Just keep in mind to add try / catch because |
Beta Was this translation helpful? Give feedback.
the SSR docs show how to use
prefetchQuery
, but you can also dofetchQuery
. It will return the data, and you and you can then use it to fetch some more.Just keep in mind to add try / catch because
fetchQuery
also throws errors.