Uploaded image for project: 'Realm JavaScript SDK'
  1. Realm JavaScript SDK
  2. RJS-2642

Allow usage of realm outside of react components

      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:

      Unable to find source-code formatter for language: tsx. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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:

      Unable to find source-code formatter for language: ts. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
        /**
         * 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

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: