kikko-land / kikko

Powerful SQLite adapter for web, mobile and desktop. Build reactive UI on top of it
https://kikko-doc.netlify.app/
MIT License
134 stars 8 forks source link

Accessing db object in react router loaders and actions #157

Open kdabir opened 1 year ago

kdabir commented 1 year ago

I am trying to fire queries in react router loaders / actions. Since loader/actions functions are neither react hooks, nor they are components, I cannot call hooks like useDbQuery in these functions. I get this error if I do:

image

I cannot access the db object as that is created/managed by the DbsHolder and is only available inside hooks. I only pass the config at the instantiation

<DbsHolder defaultDbConfig={config}>
    <EnsureDbLoaded fallback={<span>Loading db...</span>}>
...

So, to fix this:

  1. either DbsHolder accepts a db object, that I can create and pass to it
  2. or, provide some other way to access the db object in loaders/action.
quolpr commented 1 year ago

@kdabir yep, you are right, we should have the way to access dbs without DbsHolder. The idea of

either DbsHolder accepts a db object, that I can create and pass to it

Looks preferable to me, but then also you will need somewhere to add the code to close db on page leave (if necessary). Currently, DbsHolder has useEffect which stops db on demount.

Not sure about deadlines for now though