kekeee
kekeee

Reputation: 475

queryFn must be return promise using react-query?

I use react-query. The result value should be determined according to the query key. However, when it is a specific query key, api calls should not be made.

Is there any problem with the code below? I'm worried about breaking the react-query rules. please help me.

function useTest(a,b) {
  const {
    fetchNextPage,
    data,
  } = useInfiniteQuery(['test', a, b], ({ pageParam = 1 }) => {
    
    if (b=== 3) { return; }

    return fetchTest(pageParam, a)
  }, {
    cacheTime: 0,
    staleTime: Infinity,

  });

}

Upvotes: 1

Views: 3683

Answers (1)

TkDodo
TkDodo

Reputation: 28978

I think react-query will wrap the result in a Promise anyways, so you can do:

useQuery(key, () => "foo")

as well. In your example, just be aware that data can then be undefined. It might be better to disable the query in this case:

useInfiniteQuery(['test', a, b], fetchTest, { enabled: b !== 3 })

Upvotes: 2

Related Questions