Unexpected re-renders with useSuspenseQuery #8685
Unanswered
bloveridgerowe
asked this question in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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
useSuspenseQuery
does 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
AuthProvider
which internally usesuseSuspenseQuery
. The query callsgetCustomerDetails
which calls the/me
endpoint on the backend. This endpoint will return200 OK
with the user's details if they have the correct auth cookies in place, or401 Unauthorized
if not. I catch this error and returnnull
if 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:
queryFn
useSuspenseQuery
AppLayout
is calledWhat's further confusing me is why the 3 logged values returned from
useSuspenseQuery
are 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
useSuspenseQuery
throws to the nearest<Suspense>
, I would have expected only 1console.log
for the returned values, and 1console.log
forRendered 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
setTimeout
to 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
AuthProvider
as 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