Quick Start with Sync - React Native SDK
On this page
Overview
This page contains information to quickly get Realm Database integrated into your app. Before you begin, ensure you have:
- Created a Realm app
- Enabled Realm Sync
- Installed the Node SDK to use with your Realm Native app.
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.
Import Realm
At the top of your source files where you want to use MongoDB Realm, add the following line to import the SDK.
Initialize the App
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.
Define Your Object Model
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.
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.
Authenticate a User
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.
Open a Realm
Once you have enabled Realm Sync and authenticated a user, you can open a synced realm:
Create, Read, Update, and Delete Objects
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:
Watch for Changes
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:
Log Out
Once logged in, you can log out: