0

I'm using React Query for async state management in my React Native app. I've a useQuery() hook with following query options:

useQuery Hook in useStudentAssignments.ts:

const data = useQuery(
  studentAssignmentKeys.list({
    assignedToIdEq: studentId
  }),
  async ({ queryKey: [{ params }] }) => {
      // Function to persist assignment media
  },
  {
    enabled: !!assignment && !!isConnected,
    cacheTime: Infinity,
    staleTime: Infinity,
    retry: 0,
    refetchOnReconnect: 'always',
    refetchOnMount: 'always',
    onSuccess(data) {
      // Async storage of assignments
  },
  onError() {
      // show error message
  }
}

App.tsx:

In the main screen I'm using the useQuery hook as:

const { data, isLoading, isSuccess, isError, refetch, isStale } = useStudentAssignments(
    studentAssignment?.id
  );

useEffect(() => {
  if (!!isConnected) {
        refetch();      
 }, [refetch, isConnected);

}

I've these questions:

  1. Since, I've enabled option set, my understanding is that I can't use QueryClient.invalidateQueries(), since it will have no effect. How else can I mark the query key as stale so that it can automatically be refresh?

  2. If automatic query refresh isn't possible, how can I refresh it on some condition or state change?

  3. useQuery() method has an async function (queryFn). Does this function runs only once, or on intervals/ every fetch?

1 Answer 1

1
  1. the invalidateQueries, refetchQueries is ignored only when the enabled is false. So when it becomes true, those function could work as your expected
  2. you could call refetch to manually update your data. In RN, it would be called when screen/components is focused.
  3. As I know, it would be triggered when an instance of useQuery is mounted or refetch called. However, they would save the result into cache, then within the cache timeout, it would return the data from cache when you call that from second instance, while queryFn is still running on background. When completed, it updates the cache data.
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the explanation, make sense! In my setup I've staleTime:Infinity. Does this has any bearing on refetch() and cache? My understanding is that if e.g. staleTime:1000*60, then any refetch() issued during that minute will always return the data from cache? Is my understanding correct?
staleTime is for marking data as fresh one, when you set it to 1 minutes, and you have another instance of that query mounted or refresh called on that interval, it would return that data from cache. It also has cacheTime which manage when the data on cache will be cleared.
So if I am getting it right, staleTime:Infinity will never mark the data to be refreshed and hence will always return data from cache?
yes, it's correct

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.