Setting initialData does not stop immediate refetch #345
-
If i pass initialData to the useQuery hook, it doesn't seem to prevent an immediate call to the query function. I am using getInitialProps from next.js and when a client side route transition takes place, the query function is called twice, once from getInitialProps and then once again when the component is mounted. Is this intentional? It does not seem to occur when using swr in the same situation. Here is a short sample to illustrate what I mean exactly:
I would have expected setting the initialData to negate the first call of the query function. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Do you have a codesandbox you can show us? Our tests check for this functionality, so we'll need a reproduction example to debug it more closely. |
Beta Was this translation helpful? Give feedback.
-
Here you go: https://codesandbox.io/s/restless-feather-v9cr8 I wouldn't expect getData to be run on the first render, but it is. |
Beta Was this translation helpful? Give feedback.
-
So after looking at that codesandbox, I can confirm that this is actually intended behavior. The recommended way to stop the query from automatically doing anything at any time, is to set https://codesandbox.io/s/jovial-payne-i6qen?file=/pages/index.js |
Beta Was this translation helpful? Give feedback.
-
Whoever read this, even after 2 years. This behaviour can still be recreated and its still reality. The query of the example above has to look like the following:
|
Beta Was this translation helpful? Give feedback.
So after looking at that codesandbox, I can confirm that this is actually intended behavior.
initialData
on it's own is not enough to keep the query from refetching when it mounts, sinceinitialData
's main purpose is to allow you to set data that is available during SSR, and that's it. Once the query mounts, its default behavior is to refetch the data again.The recommended way to stop the query from automatically doing anything at any time, is to set
manual
totrue
. Toggling a query'smanual
state after the fact won't trigger a refetch, so you can use it to reliably pause a query's auto refetching functionality as I have shown in this codesandbox example:https://codesandbox.io/s/jovial-…