Docs Menu

Quick Start with Sync - React Native SDK

On this page

  • Overview
  • Import Realm
  • Initialize the App
  • Define Your Object Model
  • Authenticate a User
  • Open a Realm
  • Create, Read, Update, and Delete Objects
  • Watch for Changes
  • Log Out

This page contains information to quickly get Realm Database integrated into your app. Before you begin, ensure you have:

Note
Check Out the Tutorial

This page contains only the essential information that you need to set up a MongoDB Realm application. If you prefer to follow a guided tutorial that shows you step-by-step how to set up a working app, check out the React Native Tutorial where you'll build a mobile app that connects to the Task Tracker backend.

At the top of your source files where you want to use MongoDB Realm, add the following line to import the SDK.

To use MongoDB Realm features such as authentication and sync, you must access your Realm app using your Realm app ID. You can find your Realm app ID in the Realm UI.

If have not enabled Realm Sync or you enabled Sync with Development Mode in the Realm UI, you can define your object model directly in code.

Note
Get Schema from UI if Development Mode Disabled

If you have enabled Sync but turned off Development Mode, you can copy and paste the object model definitions that Realm generated for you from the SDKs tab in the Realm UI. You must re-enable Development Mode if you want to make changes to the object model definition from client side code.

Learn more in the documentation for creating a schema.

When you have enabled anonymous authentication in the Realm UI, users can immediately log into your app without providing any identifying information:

Realm provides many additional ways to authenticate, register, and link users.

Tip

Once you have enabled Realm Sync and authenticated a user, you can open a synced realm:

Once you have opened a realm, you can modify it and its objects in a write transaction block.

To create a new Task run the method "realm.create()" inside the "realm.write()" callback. Pass the string "Task" as the first parameter to "realm.create()". Pass a task object as the second parameter.

You can retrieve a live collection of all tasks in the realm:

You can also filter that collection using a filter:

To modify a task, update its properties in a write transaction block:

Finally, you can delete a task:

You can watch a realm, collection, or object for changes by registering event handlers with the Realm.addListener() Object.addListener() Collection.addListener() methods.

To stop watching for changes, run the "realm.removeAllListeners()" method. Alternatively, to remove a specific listener, call "removeListener()" on the object that is being watched and pass in the listener function:

Once logged in, you can log out:

←  Quick Start - React Native SDKQuick Start with Expo - React Native SDK →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.