redux-toolkit icon indicating copy to clipboard operation
redux-toolkit copied to clipboard

Am I missunderstanding RTK Query Cache feature?

Open atalayio opened this issue 1 year ago • 2 comments

When I make a request in Lazy Query, I cache the returned result and try to use the data in the cache even if the page changes. I make a request, the result is returned, and when I change the page, it appears as data: undefined, even though I set the preferCacheValu to true. What am I doing wrong? My code is given below;

  const [
    getCachedStockDocumentList,
    { data: stockDocumentListData}
  ] = mrpApi.endpoints.getApiStockDocumentStockDocumentList.useLazyQuery();

  console.log(stockDocumentListData)

//request
onClick={async () => await getCachedStockDocumentList({}, true)}

atalayio avatar Jun 13 '24 12:06 atalayio

It took me some time to understand, but I believe you should call the hook directly in the other component or pass it as a prop.

const { data, error, isLoading } = useMrpApiQuery()

In this case, it will theoretically recover directly from the cache without making a new request. Adding { skip: false } will prevent the automatic request. Theoretically I believe that it makes more sense to create a state to turn skip on and off. Which in your case makes more sense.

https://redux-toolkit.js.org/rtk-query/usage/conditional-fetching

valedobrandi avatar Jun 26 '24 14:06 valedobrandi

It took me some time to understand, but I believe you should call the hook directly in the other component or pass it as a prop.

const { data, error, isLoading } = useMrpApiQuery()

In this case, it will theoretically recover directly from the cache without making a new request. Adding { skip: false } will prevent the automatic request. Theoretically I believe that it makes more sense to create a state to turn skip on and off. Which in your case makes more sense.

https://redux-toolkit.js.org/rtk-query/usage/conditional-fetching

in my page that request not fetching automaticlly. I'm using button for fetching, when I press that button I call that request and when request response success I need the cached response, so when I change page and comeback I can see cached data but it's not, it is undefined and what I wonder am I understand wrong cache feature or is this way wrong?

atalayio avatar Jun 26 '24 15:06 atalayio

Hi I am also curious about this. I lazy fetch and navigating away and back then results in status uninitialized and data as undefined. I expect it to behave similar to the standard query in terms of using the cache.

lucien-theron avatar Sep 05 '24 12:09 lucien-theron