How to achieve loading state between fetches when using refetchInterval
#9428
Unanswered
alexchernous
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
Uh oh!
There was an error while loading. Please reload this page.
-
Scenario
Upon page load I call an API to display live data for the user. This API is not guaranteed to return anything on the first try, so I use
refetchInterval
to try 2 more times. During that time, the API can potentially generate and return live data which I'll display, or maybe it can't generate anything at all in which case an empty state will be shown.I'd like to display a loading state for the entire duration of this polling attempt: first load, subsequent refetches, and the time in between because I know there will be multiple refetch attempts.
Here's a simplified version of the code:
The above code will flicker the "Still loading..." state in and out, because
isFetching
is onlytrue
when the query is in flight. This is not ideal since I don't want to display the empty state to the user until I've exhausted my polling attempts and can be reasonably sure the API won't return any live data.Workaround
The docs helped me find this solution:
Question
I'm wondering if there's a better way to go about this using
react-query
. It feels odd to have to access the query cache via the query client for the query I'm directly working with in the same file.useQuery
result/parameter that could do the job?Potential feature request
If I understood some of the inner workings of the
queryObserver
correctly, a new boolean that looks at therefetchIntervalId
could be added to theresult
(QueryObserverBaseResult) of thecreateResult
function inquery-core
.Admittedly I think my use case is quite niche and this new state won't be useful for many scenarios; I imagine
refetchInterval
is mostly used for background polling.If that's the case, and it is too niche, perhaps the same
result
object can expose thedataUpdateCount
state (similarly to how it already exposes thedataUpdatedAt
state today). This can be used to calculate the samewillRefetch
boolean that I have in the Workaround section above without having to access the query cache manually.Beta Was this translation helpful? Give feedback.
All reactions