setQueryData seems to cause useQuery to invoke queryFn again #3283
-
I'm not confident this is a bug and isn't just my own misunderstanding, thus posting a discussion rather than creating an issue. My understanding is that if I use In practice it seems that by calling Here is a code sandbox illustrating the issue. https://codesandbox.io/s/stoic-ellis-mfebi?file=/src/Component.js One second after the component loads, the My expectation here would have been that the query counter would have stayed at 1 and that the Am I doing something wrong here or not understanding how this works? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Doing some reading around the docs it seems what I am seeing is not how things are supposed to work: https://react-query.tanstack.com/guides/updates-from-mutation-responses. |
Beta Was this translation helpful? Give feedback.
-
You're just getting a refetch because staleTime is zero and you refocus the window when you go from the code editor to the iframe on the right in codesandbox, so the background refetch is interfering with your mutation. It's a classic gotcha from https://tkdodo.eu/blog/practical-react-query#the-defaults-explained
updated sandbox: https://codesandbox.io/s/loving-easley-79wyg?file=/src/Component.js |
Beta Was this translation helpful? Give feedback.
You're just getting a refetch because staleTime is zero and you refocus the window when you go from the code editor to the iframe on the right in codesandbox, so the background refetch is interfering with your mutation. It's a classic gotcha
from https://tkdodo.eu/blog/practical-react-query#the-defaults-explained