T
TanStack3y ago
fascinating-indigo

Why does switching browser tabs for 1st time, re-render happens but not second time onwards?

I have some data that I fetched with useQuery, then I have an Overlay with timer on, with data fetched populated into this component. However, if I switch the browser tab, then come back to my app, the timer in the overlay page will restart from 0, instead of counting on while the time goes. But, from now on, if I keep switching tabs and come back, the timer will count as expected. Each time I switch, the console log indicated some refetch is happening in the background. So my question: why does it only happen for the first time, that it seems my timer is re-rendered, I am not sure whether it's because of refetch.... but why it functions as expected from the second time onwards? I am not sure if this has something to do with how it caches, would appreciate someone explains this weird behavior.
9 Replies
foreign-sapphire
foreign-sapphire3y ago
Does refetchOnWindowFocus: false give you the desired experience?
fascinating-indigo
fascinating-indigoOP3y ago
Oh, I haven't put that param in, is refetchOnWindowFocus: true the default?
foreign-sapphire
foreign-sapphire3y ago
Window Focus Refetching | TanStack Query Docs
If a user leaves your application and returns and the query data is stale, TanStack Query automatically requests fresh data for you in the background. You can disable this globally or per-query using the refetchOnWindowFocus option: Disabling Globally
foreign-sapphire
foreign-sapphire3y ago
Window Focus Refetching | TanStack Query Docs
If a user leaves your application and returns and the query data is stale, TanStack Query automatically requests fresh data for you in the background. You can disable this globally or per-query using the refetchOnWindowFocus option: Disabling Globally
fascinating-indigo
fascinating-indigoOP3y ago
But even so, why would the second onwards behaves? I am trying to understand why only first switch affects....
foreign-sapphire
foreign-sapphire3y ago
That I'm not sure, sorry
fascinating-indigo
fascinating-indigoOP3y ago
I will give that param a try and report back here. But appreciate someone explains the issue. Ok, doing this seems to be solve it.... I will read more details in the doc tmr.... and marks this as solved for now. Thanks for the help!
other-emerald
other-emerald3y ago
do you have a staleTime set?
fascinating-indigo
fascinating-indigoOP3y ago
No, I use the default setting

Did you find this page helpful?