Unexpected re-renders with useSuspenseQuery #8685
Unanswered
bloveridgerowe
asked this question in
General
Replies: 1 comment
-
|
I faced the same issue. I resolved it by using useQuery instead. |
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.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there,
I'm very new to React so do forgive me if this is a stupid question. I've searched everywhere and spent hours to try and gain an understanding of why this is happening but I've yet to find an answer.
The behaviour I'm observing with
useSuspenseQuerydoes not align with what I'm expecting. I truly would appreciate any insight you could share with me on what I'm doing wrong, or if this is indeed correct behaviour.I've created an
AuthProviderwhich internally usesuseSuspenseQuery. The query callsgetCustomerDetailswhich calls the/meendpoint on the backend. This endpoint will return200 OKwith the user's details if they have the correct auth cookies in place, or401 Unauthorizedif not. I catch this error and returnnullif this is the case (no user data, not logged in), or re-throw the error if something else has gone wrong.I have a
<Suspense>directly above it near the top of the tree to enable me to show a loading spinner while we deduce the auth state of the current user, as I don't want the webapp accessible until we know if they are logged in or not:It works correctly from a UX point of view, but I'm noticing 2 or 3 re-renders during the initial app load. Most of the time it is 3, but sometimes it's only 2, it's not 100% consistent which leads me to believe it may be a timing issue.
This can be seen from the logs in the browser console, where I log:
queryFnuseSuspenseQueryAppLayoutis calledWhat's further confusing me is why the 3 logged values returned from
useSuspenseQueryare identical. Had the values (and state) changed, then I could understand the re-render, but as they are the same I am unsure why I'm seeing this behaviour.I have also checked the network logs and verified only 1 API call is being made to
/me:Given
useSuspenseQuerythrows to the nearest<Suspense>, I would have expected only 1console.logfor the returned values, and 1console.logforRendered AppLayout.If I change it to use
useQuery, and handle the returning of a<Spinner>internally myself within theAuthProvider, then I only see 1 render as I expect:The only way I can prevent this behaviour is by deliberate adding a
setTimeoutto the query function:Then, as you can see from the console, there is only a single render. But I naturally won't be adding deliberate timeouts to my
AuthProvideras that is an awful thing to do! But (to me) it illustrates that the cause is likely a timing issue potentially, though I'm not clear on what is causing this.I do hope I haven't wasted your time asking this, I've tried very hard to figure this out for myself.
Full repository if it helps for context:
main.tsx: https://github.com/bloveridgerowe/eshop-ui/blob/main/src/main.tsxauth-provider.tsx: https://github.com/bloveridgerowe/eshop-ui/blob/main/src/components/utilities/AuthProvider.tsxapp-layout.tsx: https://github.com/bloveridgerowe/eshop-ui/blob/main/src/layouts/AppLayout.tsxThank you for your time.
Beta Was this translation helpful? Give feedback.
All reactions