Global loading state when switching organization with cookie-based auth (React Query v5) #9903
Unanswered
matinsekhavat
asked this question in
Q&A
Replies: 3 comments
-
|
i think we need sth like this BASED ON DOCS: inside the resetQueriesAlso |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
try { |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
you can show a global loading indicator with |
Beta Was this translation helpful? Give feedback.
0 replies
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.
-
Hi everyone,
We have a multi-organization dashboard. Authentication is cookie-based and when the user switches organization we call this endpoint:
TypeScriptPOST /v1/auth/switch-organization
{ organization_id: "123", use_cookie: true }
This endpoint sets a new session cookie on the browser.
The problem:
All our data is fetched with React Query (useQuery) in many different pages/components.
When the user clicks "Switch organization":
I need to show loading (skeleton/spinner) immediately in the entire app (even on pages that are already open in background tabs).
I must cancel/invalidate all previous queries so they don’t refetch with the old cookie.
Queries must refetch only after the new cookie is fully set in the browser.
If I call resetQueries / invalidateQueries before the POST → queries refetch with old cookie → wrong 401 or wrong data.
If I call them after the POST → there is a 1–3 second delay where the UI is completely frozen/stale, then suddenly loading appears → very bad UX.
Current attempt (not perfect):
TypeScriptawait queryClient.cancelQueries(); // good, stops old requests
await axios.post('/switch-organization', ...) // sets new cookie
await new Promise(r => setTimeout(r, 2000)); // ugly delay
await queryClient.resetQueries({...}) // now refetches correctly
This works, but the 2-second blank screen feels terrible.
My goal (ideal behavior):
The very moment user clicks → entire app shows loading immediately (global isPending = true)
No request goes out with the old cookie
As soon as new cookie is set → all queries refetch together with the new cookie
Solution must be global → I have 50+ pages, I don’t want to add manual state to every component
I tried:
queryClient.cancelQueries() + resetQueries() → timing issue
Local isSwitching state → works but not global (background pages don’t show loading)
enabled: false on each query → too much boilerplate
Is there a clean, global way in React Query v5 to achieve:
Immediate global loading indicator on org switch
Guaranteed refetch only after new cookie
No per-page code
Thanks a lot!
Beta Was this translation helpful? Give feedback.
All reactions