How to access "realm" in onError of RealmProvider sync configuration

I am using realm/react in react native, but I have a problem, and that is, in the documentation, it shows an example, where a function is passed to the onError property of the RealmProvider synchronization configuration object, this function closes realm and returns it to open, my problem is that in the example you can see that the realm object is used, from useRealm, but the configuration is just being defined, also it cannot be inside a component or hook, it must be an independent object.

Resume: If you see the code, there is a declaration at the beginning:

let realm; // value assigned in with useRealm().

But I don’t understand, how from syncConfigWithClientResetFallback, I can access the “realm” variable defined in RestOfApp. (Line 15)

    let realm; // value assigned in <RestOfApp> with useRealm()
    const syncConfigWithClientResetFallback = {
      flexible: true,
      clientReset: {
        mode: Realm.ClientResetMode.RecoverOrDiscardUnsyncedChanges, // or "recoverUnsyncedChanges"
        // can also include `onBefore` and `onAfter` callbacks
        onFallback: (_session, path) => {
          try {
            // Prompt user to perform a client reset immediately. If they don't,
            // they won't receive any data from the server until they restart the app
            // and all changes they make will be discarded when the app restarts.
            const didUserConfirmReset = showUserAConfirmationDialog();
            if (didUserConfirmReset) {
              // Close and delete old realm from device
              realm.close();
              Realm.deleteFile(path);
              // Perform client reset
              Realm.App.Sync.initiateClientReset(app, path);
              // Navigate the user back to the main page or reopen the
              // the Realm and reinitialize the current page
            }
          } catch (err) {
            // Reset failed. Notify user that they'll need to
            // update the app
          }
        },
      },
    };
    function RealmWithManualClientResetFallback() {
      return (
        <RealmProvider sync={syncConfigWithClientResetFallback}>
          <RestOfApp />
        </RealmProvider>
      );
    }
    function RestOfApp() {
      // Assigning variable defined above to a realm.
      realm = useRealm();
      return <>{/* Other components in rest of app */}</>;
    }

This topic was automatically closed after 180 days. New replies are no longer allowed.