component refetches on mount even after data is prefetched #1369
Unanswered
brettgoulder
asked this question in
Q&A
Replies: 1 comment
-
In v3, you don’t set the stale time in the prefetch for the future. Stale time on a prefetch determines whether the prefetch should happen based on when the data was previously updated. So if there is already cached data and it is 5 seconds old, this prefetch will be skipped: prefetchQuery(key, fn, {staleTime: 6000}) And the same applies for useQuery. So what you are probably looking for is this: prefetchQuery(key, fn) This way, as long as the data is less than a second old, the useQuery will not trigger another fetch. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the bug
I have been creating a basic CRUD app architecture using react-query. I ran into an issue when utilizing prefetch, where it makes an extra fetch after the new component is mounted, even though I am setting staleTime in the prefetch.
Is this because the prefetched query becomes inactive when the new component mounts? Is it intended behavior to refetch in this case?
To Reproduce
Steps to reproduce the behavior:
Expected behavior
I expect for the refetch to not happen because I have already prefetched the data.
* note this code is v3
Here is the prefetch in the source code: https://codesandbox.io/s/competent-cohen-7igge?file=/src/pages/UserList.jsx:973-1231
Here is the custom hook being called on the Update User form: https://codesandbox.io/s/competent-cohen-7igge?file=/src/pages/UpdateUser.jsx:303-363
Desktop (please complete the following information):
Beta Was this translation helpful? Give feedback.
All reactions