Replies: 3 comments 3 replies
-
I'm trying to wrap my head around this too. Here is an example https://codesandbox.io/s/wizardly-leakey-cybh5?file=/src/index.js There are two fetch requests made. I would assume that there should be 5 request made but there are 2?. What would be a good approach to insuring only one request is made? Setting the global |
Beta Was this translation helpful? Give feedback.
-
I think I'm having the same problem as you. just to be clear, you want the hook on component |
Beta Was this translation helpful? Give feedback.
-
I don't really understand the use-case. Either you want data to be up-to-date, or not ;) I don't understand why it matters so much how the refetch was triggered (mounting of a new subscriber, window refocus, something else ...). I usually look at it like this:
That's what looking at your component A and component B example, I'd like to see the components more in isolation. If component B mounts 20 minutes after component A, why would we not want a re-fetch?. On the other hand, if you switch routes back and forth within 10 seconds, why would you want a refetch? So it's not the action that matters so much, it's the timing. At least that's how I'm seeing it :) |
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.
-
Lets say I have a component A that uses a query (wrapped in a custom hook that we will call useUser). Since I have the default config of staleTime set to 0, this query is stale as soon as it mounts.
Now, component A renders down the tree a bit further another component (component B) that needs the same data, so I use the same custom hook useUser here. This component is not mounted immediately after component A mounts, but is mounted after an user action.
Now, since the query from component
A
is stale, when component B mounts, the query refeches. This is my current problem. I don't wantuseUser
to trigger a refetch when component B mounts as long as componentA
is still active.A solution for this may be to play with the
staleTime
option, but I want to keep this to 0 since it allows me to always have up-to-date date when I change a route without the need of invalidating any query in another route. (I have a personal rule of only caring to invalidate currently active queries to have them refetch).Another solution is just set
refetchOnMount: false
in componentB
, but I want this behavior to be the default. Also, there is a possible situation in which I mount componentB
without mounting componentA
, and sincerefetchOnMount
is false, the query may not trigger (unless it is the first time the query appears).So, basically what I want is a
refetchOnMount
that can be set tostale_and_inactive
. I only want to do a refetch on mount when a query is both stale and inactive (or not in the cache).Is there a way to do this? It actually feels like a
bug
to me.react-query
is a state manager for server side data, but that ability is reduced if I cannot use the same query in several components without triggering a HTTP request.P.D: this is a more elaborated discussion from our discussion on twitter @TkDodo (btw, huge thanks for the support there!)
Beta Was this translation helpful? Give feedback.
All reactions