I don't understand how keepPreviousData works #3640
Replies: 3 comments 1 reply
-
Hello All, |
Beta Was this translation helpful? Give feedback.
-
In your example sandbox, you will only see |
Beta Was this translation helpful? Give feedback.
-
i tried using keepPreviousData but it seems it return undefined. i used it in useQuery |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey! I'm trying to understand how
keepPreviousData
works. I wanted to use it like this:keepPreviousData
) and replace it with new data once that comes in, without showing a spinner(we can assume the query keys just go from "1" -> "2" -> "1")
Now what I don't understand it that
isPreviousData
seems to betrue
whenever I fetch new data, but isfalse
when I re-fetch data/cache key that I have already fetched before. Shouldn't this be the inverse? Because if we refetch a previously fetched cacheKey in the background, it would show the "previous data" in the meantime?I have found a CodeSandbox that I modified slightly to show what I mean: https://codesandbox.io/s/isloading-vs-isfetching-when-using-keeppreviousdata-forked-qiixhg?file=/pages/index.js
If we go back and forth there, between page 1 and 2 (hitting nextPage/prevPage constantly)
isFetching
will betrue
every 5s., and the other booleans will be false. But if we go to a page we didn't open previously,isPreviousData
will also be true until the new data loads, shouldn't this be the opposite? I'm confused.Additionally I don't think I understand the interactions between stale data, cached data and previousData (and potentially initialData and placeholerData) is there a guide somewhere that explains it a bit better? I've searched, but couldn't find something that would clear it up for me.
Beta Was this translation helpful? Give feedback.
All reactions