skip to Main Content

I know that React query calls the api with useQuery to turn the data into a map and display it on the screen.

export const getList = async() => { 
  const response = await api.get(/~/~/~/list); 
  return response.data; 
}; 
export const getNewList = async() => { 
  const response = await api.get(/~/~/~/Newlist); 
  return response.data; 
};

If there are two things to import, how should I use useQuery on the page? I’m Korean and I’m sorry if the translation is strange, the api was specified by the backend We are making a practice project I called useQuery twice const { isLoading, isError, data } = useQuery() Called twice in this way, it turned out that block scope variables could not be declared again.

2

Answers


  1. you can write it simply like this

    const getListQuery = useQuery(...)
    const getNewListQuery = useQuery(...)
    

    then you can use the variables from it

    if (getListQuery.isLoading) {
     ...
    }
    ...
    
    Login or Signup to reply.
  2. would be great if you could provide an example code snipped in codebox, so it is easier to see exact problem. If i understand correctly, your problem is that you cannot use same variable name twice, so what you can do is destruct the response and rename it.

    { isLoading, isError, data } = useQuery() 
    { isLoading: isLoadingSecondQuery, isError: isErrorSecondQuery, data: dataSecondQuery } = useQuery()
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search