query icon indicating copy to clipboard operation
query copied to clipboard

cacheTime loading states

Open iamjohnlong opened this issue 6 years ago • 8 comments

Describe the bug Setting cacheTime: 0 doesn't set the loading state to true when refetched

To Reproduce Steps to reproduce the behavior:

  1. Go to https://codesandbox.io/s/ecstatic-cray-uobv0?file=/src/App.tsx
  2. Click on the two link and watch the console
  3. isLoading is never set to true when clicking back and forth from root to two routes

Expected behavior If setting cacheTime: 0 I would expect that the fetch gets recalled and the loadings states are true until the fetch resolves.

Desktop (please complete the following information):

  • OS: macOS
  • Browser: chrome
  • Version: 86.0.4240.111

Additional context Add any other context about the problem here.

iamjohnlong avatar Oct 24 '20 14:10 iamjohnlong

Thanks for reporting! Fixed in 3.2.0-beta.12

boschni avatar Oct 24 '20 16:10 boschni

@boschni will this be fixed in v2?

iamjohnlong avatar Oct 24 '20 17:10 iamjohnlong

@boschni any thoughts on this being fixed in v2? Sorry to bug I have a large project in running v2.

iamjohnlong avatar Oct 26 '20 14:10 iamjohnlong

Sure you are not able to use staleTime 0 together with isFetching?

Op 26 okt. 2020 om 15:23 heeft John Long [email protected] het volgende geschreven:

 @boschni any thoughts on this being fixed in v2? Sorry to bug I have a large project in running v2.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

boschni avatar Oct 26 '20 14:10 boschni

I've tried both

const { isLoading, error, data } = useQuery(
    "repo",
    () =>
      fetch(
        "https://api.github.com/repos/tannerlinsley/react-query"
      ).then((res) => res.json()),
    {
      cacheTime: 0,
      staleTime: 0
    }
  );

and

const { isLoading, error, data } = useQuery(
    "repo",
    () =>
      fetch(
        "https://api.github.com/repos/tannerlinsley/react-query"
      ).then((res) => res.json()),
    {
      staleTime: 0
    }
  );

with the same issue. https://codesandbox.io/s/optimistic-bohr-bh9dk?file=/src/App.tsx

It also gets stuck in a infinite loop making requests on iOS safari.

iamjohnlong avatar Oct 26 '20 15:10 iamjohnlong

What if you try to use isFetching instead of isLoading?

boschni avatar Oct 26 '20 15:10 boschni

@boschni things are working correctly here https://codesandbox.io/s/goofy-tharp-h5ifu

I had to update the cache key to use an id ["repo", id]. Using a static query key like "repo" seems to not respect cacheTime or staleTime, not sure if that's by design?

iamjohnlong avatar Oct 26 '20 15:10 iamjohnlong

Changing the query key should not matter, but the cacheTime is generally not used and should not be changed most of the time, staleTime: 0 should be enough.

boschni avatar Oct 26 '20 15:10 boschni