What's the best approach to trigger a new network request for newly mounted query when having staleTime
set
#5540
Unanswered
mk-nickyang
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi Team, I'm new to the TansStack Query, and these are the two cases I came across so far where I find setting
staleTime
is useful.Case 1 - I do NOT want a new network request each time user enters the same screen.
So I set a 60-second
staleTime
, and no network requests will be triggered when re-entering the same screen in a short period. Perfect.Case 2 - I DO want a new network request each time user enters the same screen, but I also have multiple instances of
useQuery
mount in different render cycles in the screen.So first, I set a 20-second
staleTime
to deduplicate network requests for instances ofuseQuery
mount in different render cycles, like what TkDodo said here.But what if the user re-enters the screen within 20 seconds? I do want a new network request right, so the best solution I can think of is to manually invalidate queries.
I guess this works fine, I can just invalidate queries when the screen unmounted. But I can see devs will forget to do this, so I'm not entirely sure if this is a textbook solution.
Also, what if I have a new query spawned in a lazily-loaded tab, then the current screen won't unmount, so I have to also invalidate queries in
useFocusEffect
. It just feels like there will be a place where I would forget to invalidate these queries.Anyway, just want to confirm if my understanding to
staleTime
is correct and if manually invalidate queries is the correct way for case 2. Thanks a lot 🙏Context: I'm working on a React Native project, that's why I use terms like 'screen' and 'tab'
Beta Was this translation helpful? Give feedback.
All reactions