Client "piggybacking" does not work, which causes skipped re-renders on many sign-in operations #4372
Open
4 tasks done
Labels
needs-triage
A ticket that needs to be triaged by a team member
Preliminary Checks
I have reviewed the documentation: https://clerk.com/docs
I have searched for existing issues: https://github.com/clerk/javascript/issues
I have not already reached out to Clerk support via email or Discord (if you have, no need to open an issue here)
This issue is not a question, general help request, or anything other than a bug report directly related to Clerk. Please ask questions in our Discord community: https://clerk.com/discord.
Reproduction
—
Publishable key
—
Description
I apologize for not providing a minimal reproduction, I am currently in a rush. To compensate, I did some deep debugging and have identified the cause of the bug, which I describe below.
Issue
In any client-side React runtime,
signIn.attemptFirstFactor()
does not cause a re-render of theuseSignIn()
hook, meaning that components who are subscribed to that hook do not get re-rendered. This also happens for various other signIn operations as well, but this is the one that sent me down a rabbit hole for hours.The reason this happens is because of the following function, which is supposed to ultimately trigger the re-render but actually doesn't:
javascript/packages/clerk-js/src/core/resources/Base.ts
Lines 79 to 90 in 8ddb3ea
The issue is on line 88:
BaseResource.clerk
andClient.getInstance()
resolve to the same object. Because of this, the same Client object gets mutated instead of a copy being created, and therefore, the React memoization logic fails.The React memoization logic I'm referring to is the stateWithMemoizedResources function, which invokes
clientChanged
to determine if the client object has changed. Although the logic ofclientChanged
is correct, because of the above, the function actually always returnsfalse
, even if the client was updated. The reason this is happening is becauseprev
andnext
are the same object.javascript/packages/clerk-js/src/utils/memoizeStateListenerCallback.ts
Lines 22 to 28 in 8ddb3ea
How to reproduce this issue
I will fill this in later if needed. However, the above should be easy to validate by checking that
prev === next
in every invocation ofclientChanged
.Environment
The text was updated successfully, but these errors were encountered: