Using Realm with redux

Hello Tony,

I would highly recommend against putting realm collections and/or items in a redux store, as Realm objects are self-mutating objects, and therefor wont play well with the principles of redux.

That said, there are some issues with the code snippet you supplied:

Realm.User is not a user instance, it’s the class defining a realm-user.

You seem to never actually authenticate a user for sync.
Please see: https://docs.mongodb.com/realm/react-native/quick-start/#authenticate-a-user

So you’ll first need to determine how your users will authenticate? email/password, facebook etc?

When you have a logged in user instance, this can be used in the sync config (and not Realm.User.prototype. If this is in our docs or examples somewhere, please let me know).

Theoretical example (this uses anonymous login):

const openSyncedRealm = async () => {
  const app = getRealmApp();
  const user = await app.logIn(Realm.Credentials.anonymous());
  const realm = await Realm.open({
    schema: [ItemSchema],
    sync: {
      user,
      partitionValue: user.id,
      error: (e) => {
        console.log(e);
      },
    },
  });

  return realm;
};
2 Likes