1

Using react-query v.3, what's the best way to get the data of a promise response of a useQuery() dependant query?

I cannot map() directly to the data response.

ref.: https://react-query.tanstack.com/guides/dependent-queries

1 Answer 1

1
  • you can check if data is available via isSuccess:
 const { isSuccess, data } = useQuery(
   ['projects', userId],
   getProjectsByUser,
   {
     // The query will not execute until the userId exists
     enabled: !!userId,
   }
 )
 
 if (isSuccess) {
    return data.map(...) // <-- data is guaranteed to be defined
 }
 const { isSuccess, data } = useQuery(
   ['projects', userId],
   getProjectsByUser,
   {
     // The query will not execute until the userId exists
     enabled: !!userId,
   }
 )
 
 return data?.map(...) ?? null // <-- make sure to handle fallback with ??
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.