fix(react): usePGlite returns null instead of throwing outside provider#953
Open
claygeo wants to merge 2 commits intoelectric-sql:mainfrom
Open
fix(react): usePGlite returns null instead of throwing outside provider#953claygeo wants to merge 2 commits intoelectric-sql:mainfrom
claygeo wants to merge 2 commits intoelectric-sql:mainfrom
Conversation
Previously calling usePGlite() without a PGliteProvider ancestor threw a runtime error. This made it impossible to use pglite conditionally or in components that render before the provider mounts. Change the hook to return null when no provider is found. The type signature is updated from T to T | null so TypeScript enforces null checks at the call site — callers guard with `if (!db) return`. The explicit-db override path (usePGlite(db)) is unchanged. Fixes: electric-sql#878
Since usePGlite now returns null outside a provider instead of throwing, hooks that call db.live.query() would crash with a TypeError on null dereference. Add an early return in the useEffect so subscriptions are simply skipped when no provider is mounted.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Calling
usePGlite()without a<PGliteProvider>ancestor throws a runtime error:This makes it impossible to use pglite hooks conditionally or in components that render before the provider mounts — a common pattern for lazy loading, SSR, or optional database features.
Fixes #878
Fix
Return
nullinstead of throwing when no provider is found:The return type is updated from
TtoT | null, so TypeScript enforces null checks at the call site:The explicit-db override path
usePGlite(db)is unchanged.Live query hooks
useLiveQueryImplinhooks.tscallsdb.live.query()unconditionally. With this change,dbcan benull, so a null guard is added to theuseEffectto skip subscriptions when no provider is mounted:Tests
Added 3 tests covering: null return outside provider, no-throw outside provider, explicit db arg passthrough.