1

I have a problem where useQuery is always running in my application and I don't why

In my component

import { GET_DATA } from 'apiCalls';
const {  loading, error, data } = useQuery('getData', GET_DATA(token));

In my api call

export const GET_DATA = async (token) => {
try {
    const res = await axios.get(`${process.env.REACT_APP_SERVER}/api/...`, {
        headers: {'auth-token': token},
    });
    console.log(res);
    return res.data;
} catch (err) {
    console.log('Error getting data');
    return err;
}

}

when I debug my app. The function GET_DATA is always running ALL the time. what is the issue here ?

4
  • I think in some point in your app you are using useEffect and inside the useEffect you are doing something that makes the component to rerender. if you provide more information on the component that you are using GET_DATA in it, I can tell you where is the problem. Commented Aug 6, 2021 at 18:38
  • I Don't even have useEffect in my component @Ako Commented Aug 6, 2021 at 18:45
  • I checked my component and the problem is useQuery 100% since when I comment it the component stop always rerendering @Ako Commented Aug 6, 2021 at 18:51
  • you must not call the function inside useQuery. I write the answer how to use it. Commented Aug 6, 2021 at 19:20

3 Answers 3

3

You must provide the useQuery only the function it wants to run, you must not call it inside useQuery. Provide the token to GET_DATA this way:

EDIT

As @tkdodo said we don't need to use the async function.

const {  loading, error, data } = useQuery('getData', ()=>{
   return GET_DATA(token);
});

The first solution I provided was this:

const {  loading, error, data } = useQuery('getData', async()=>{
   const data = await GET_DATA(token);
   return data;
});

Sign up to request clarification or add additional context in comments.

2 Comments

This syntax is unnecessary convoluted as opposed to just () => GET_DATA(token)
@TkDodo Yes, you are right, we don't need to use async function.
1

The root cause is the same as in React-Query, useQuery returns undefined only after loading is complete

The queryFn needs to be a function that returns a promise. GET_DATA does that. But by doing

GET_DATA(token) you directly invoke the function. So you’ll likely want:

() => GET_DATA(token) instead.

Comments

0

Try the following:

// apiCalls.js
export const getData = async (token) => {
  try {
    const res = await axios.get(`${process.env.REACT_APP_SERVER}/api/...`, {
        headers: {'auth-token': token},
    });
    return res.data;
  } catch (err) {
    console.log('Error getting data');
    return err;
}

// Component.js
import { getData } from 'apiCalls';

function Component(){
  const {  loading, error, data } = useQuery(
    'getData', 
    ()=>GET_DATA(token)
  );

  return (
    <div>...</div>
  )
}

useQuery should run in the component and the second parameter should not be a promise, but a function that returns a promise.

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.