Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useLiveQuery doesn't work with makePGliteProvider. #543

Open
thruflo opened this issue Feb 17, 2025 · 0 comments
Open

useLiveQuery doesn't work with makePGliteProvider. #543

thruflo opened this issue Feb 17, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@thruflo
Copy link
Contributor

thruflo commented Feb 17, 2025

The point of the PGliteProvider is:

to initiate a PGlite database and pass it to all child components for use with the usePGlite, useLiveQuery, and useLiveIncrementalQuery hooks.

However, makePGliteProvider only returns usePGlite:

const { PGliteProvider, usePGlite } = makePGliteProvider<
  PGlite &
    PGliteInterfaceExtensions<{...}>
>()

This means when you make a type-aware PGliteProvider like this, it's not recognised by useLiveQuery and friends.

Uncaught Error: No PGlite instance found, use PGliteProvider to provide one

Here's a minimal repro:

import React, { useEffect, useState } from 'react'
import { createRoot } from 'react-dom/client'
import { PGlite, PGliteInterfaceExtensions } from '@electric-sql/pglite'
import { makePGliteProvider, useLiveQuery } from '@electric-sql/pglite-react'
import { type LiveNamespace, live } from '@electric-sql/pglite/live'

type PGliteWithExtensions = PGlite & PGliteInterfaceExtensions<{
  live: LiveNamespace
}>

const { PGliteProvider, usePGlite } = makePGliteProvider<PGliteWithExtensions>()

function App() {
  const [db, setDb] = useState()

  useEffect(() => {
    async function init() {
      const pglite: PGliteWithExtensions = await PGlite.create({
        extensions: {
          live,
        }
      })

      setDb(pglite)
    }

    init()
  }, [])

  if (db === undefined) {
    return null
  }

  return (
    <PGliteProvider db={db}>
      <Component />
    </PGliteProvider>
  )
}

function Component() {
  const results = useLiveQuery('select 1')

  return null
}

export function renderApp(el) {
  createRoot(el).render(
    <App />
  )
}

// const el = document.getElementById('app')
// renderApp(el)
@thruflo thruflo added the bug Something isn't working label Feb 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant