D
D
DeniSidorenko2021-04-19 08:30:22
React
DeniSidorenko, 2021-04-19 08:30:22

How to use 2 useQuery in one file?

Good afternoon, I use Graphql + Apollo client on react'e. For example, when I have one request in a file, I can arrange it this way

const { loading , data = {} } = useQuery(KINDS)
  const { Kinds = [] } = data
// и дальше к примеру 
if(!loading){
  // code
}


But what about when I have 2 requests in one file, and the variables loading, data can no longer be used. And then you have to write long constructions.
Example
const kindsByRestaraunt = useQuery(KINDS_BY_RESTAURANT, {
    variables: { id }
  })

  if(!kindsByRestaraunt.loading)
    console.log(kindsByRestaraunt?.data?.KindsByRestaurant)


How can the first syntax be reused?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-04-19
@DeniSidorenko

You can rename variables when destructorizing.

const person = {
  name: 'John',
  age: 23
};

const { name } = person;
const { name: personName } = person;
console.log(name); // John
console.log(personName); // John

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question