Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple usequeries in a single component

I am unable to get the data by using the useQuery in the below mentioned way

const { data:data1 } = useQuery(Get_Doctor);
const {  loading, error,data } = useQuery(GET_Branch);
like image 725
Monika Avatar asked Aug 20 '19 07:08

Monika


People also ask

What is the difference between useQuery and useLazyQuery?

Unlike with useQuery , when you call useLazyQuery , it does not immediately execute its associated query. Instead, it returns a query function in its result tuple that you call whenever you're ready to execute the query.

How does useQuery work?

We run a query within a React component by calling useQuery and passing it our GraphQL query string. This makes running queries from React components a breeze. When our component renders, useQuery returns an object from Apollo Client that contains loading , error , and data properties that we can use to render our UI.

What is useMutation in react query?

useMutation will track the state of a mutation, just like useQuery does for queries. It'll give you loading, error and status fields to make it easy for you to display what's going on to your users. You'll also get the same nice callbacks that useQuery has: onSuccess, onError and onSettled.


1 Answers

Indeed you can use multiple queries

const queryMultiple = () => {
  const res1 = useQuery(Get_Doctor);
  const res2 = useQuery(GET_Branch);
  return [res1, res2];
}

const [
    { loading: loading1, data: data1 },
    { loading: loading2, data: data2 }
] = queryMultiple()

What you want to do is compose multiple useQuery into your own custom hook:

Reference

But still, its just a syntactic sugar

like image 54
Medet Tleukabiluly Avatar answered Oct 29 '22 20:10

Medet Tleukabiluly