-
Type: Improvement
-
Resolution: Unresolved
-
Priority: Minor - P4
-
None
-
Affects Version/s: None
-
Component/s: Realm React
Problem
When using @realm/react, it provides you with a ... Provider. And in child components you can use hooks to get access to your realm.
However, I want to be able to access the realm in other parts of the application that are decoupled from the react components.
I dont see a way how to do that since the realm created in the provider is always closed access
Solution
Allow to pass in a realm to the Provider that is used instead of the automatically created one.
This realm can then be used for functionality outside of react
Alternatives
Atm, I pass in a custom object to the realmRef prop that is filled with the realm and share that custom object to other parts of my application:
export const realmRef: { current: Realm | null } = { current: null } // later <RealmProvider realmRef={realmRef} />
However, this leads to problems with hot reloading and is really hacky
How important is this improvement for you?
I would like to have it but have a workaround
Feature would mainly be used with
Local Database only
// Edit:
I found this comment for realmRef in your code:
/**
* A ref to the Realm instance. This is useful if you need to access the Realm
* instance outside of a component that uses the Realm hooks.
*/
So, it seems like I am using realmRef correctly - kinda. Because you cannot use useRef outside of react components to create a ref (i faked it). Also: Hot reloading breaks. If i have to guess its because a new realm is created on rerender and the old one is still around somehow