@clerk/react: with a preloaded Clerk instance, hooks are not loaded on the first render and <ClerkLoaded> never renders

Offen
#10,532 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Anfängerfreundlichkeit
58/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Aktiv
Tech-Stack
react, typescript

Rechercherichtung

Start by locating ClerkProvider, useAuth(), and ClerkLoaded in the React package, then trace how a preloaded Clerk instance initializes provider state and notifies subscribers. Reproduce with the listed package versions and the three steps in the issue. Done means the first render reflects the loaded instance and ClerkLoaded renders; the issue does not name a test file.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

Related: #8569 (a preloaded instance passed to <ClerkProvider Clerk={clerk}> skips the UI load). We work around that by passing the UI at our own clerk.load({ ui: { ClerkUI } }). That leaves this gap.

Problem

With a preloaded instance passed as <ClerkProvider Clerk={clerk}>:

  • On the first render, useAuth() returns isLoaded: false and useOrganization() has no membership, although the instance is already loaded. They become loaded one tick later, when the provider takes the instance over.
  • <ClerkLoaded> does not re-render when that happens, so it cannot be used to wait for it.

A page that reads the role on its first render (for example, an admin-only route on a full page load) sees "not an admin" and redirects away.

Repro

@clerk/clerk-js 6.42, @clerk/react 6.18, @clerk/ui 1.42.

  1. const clerk = new Clerk(pk); await clerk.load({ ui: { ClerkUI: ui.ClerkUI } }); before createRoot.
  2. Render <ClerkProvider Clerk={clerk}><Probe /></ClerkProvider>.
  3. In Probe, log useAuth().isLoaded: the first render logs false. Wrap Probe in <ClerkLoaded> and it never renders.

Proposal

With a loaded instance, the provider starts with isLoaded: true and the session, user and organization already set. Or <ClerkLoaded> re-renders when the provider becomes loaded.

Our workaround

One wrapper at the root renders the app only when useAuth().isLoaded is true.

(issue generated with AI assistance)

Vorherrschende Sprache
TypeScript
Sterne
1.8k
Forks
492
Ø Merge
1 T. 7 Std.
Gemergte PRs (30 T.)
357

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus clerk/javascript

Alle Issues in clerk/javascript

Ähnliche Issues

Weitere Issues zu TypeScript

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.