Navigation

Install Realm for React Native

Overview

The MongoDB Realm React Native SDK enables development of React Native applications using the JavaScript and TypeScript languages. React Native enables you to build cross-platform iOS and Android apps with a single codebase using the React framework.

Prerequisites

Before getting started, ensure your development environment meets the following prerequisites:

Expo does not support Realm

Unfortunately, Expo does not support Realm. If you use the Expo CLI or create-react-native-app to create your React Native project, you will need to “eject” your Expo project to use Realm. The installation steps on this page do not use the Expo CLI.

Installation

Select the tab below that corresponds to your React Native version. Follow the steps to create a React Native project and add the Realm React Native SDK to it.

1

Create a React Native Project

Create your React Native project with the following command:

npx react-native init MyRealmApp

Change to the project directory that the previous command just created:

cd MyRealmApp
2

Install Realm with NPM

In your React Native project directory, add Realm to your project with the following command:

npm install --save realm@10.0.0-beta.6
3

Resolve CocoaPods Dependencies

For the iOS app, fetch the CocoaPods dependencies with the following commands from your React Native project directory:

cd ios && pod install && cd ..

This downloads the Realm libraries and regenerates the project .xcworkspace file that you can work with in Xcode to run your application.

4

Enable TypeScript (optional)

TypeScript is a superset of JavaScript that adds static type checking and other features intended to make application-scale development more robust. If you’d like to use TypeScript in your project, follow the React Native team’s official TypeScript and React Native guide. Realm supports TypeScript natively and integrates easily into a TypeScript project.

5

Run the App

React Native enables simultaneous development of both an iOS and Android app that use the same React codebase. You can edit the .js or .ts source files in your project directory to develop your app.

In development, the apps read their React source code as a bundle from a local bundle server. To run the bundle server, use the following command in your React Native project directory:

npm start

With the bundle server running, you can now launch the Android and iOS apps:

  • To run the Android app, use Android Studio to open the android directory in your project directory and click Run.
  • To run the iOS app, use Xcode to open the .xcworkspace file in the ios directory. If you did not use CocoaPods during setup, open the .xcodeproj file in the ios directory instead. Once you have opened the project, click Run.
1

Create a React Native Project

Create your React Native project with the following command:

react-native init MyRealmApp

Change to the project directory that the previous command just created:

cd MyRealmApp
2

Install Realm Using NPM

In your React Native project directory, add Realm to your project with the following command:

npm install --save realm@10.0.0-beta.6
3
4
5

Enable TypeScript (optional)

TypeScript is a superset of JavaScript that adds static type checking and other features intended to make application-scale development more robust. If you’d like to use TypeScript in your project, follow the React Native team’s official TypeScript and React Native guide. Realm supports TypeScript natively and integrates easily into a TypeScript project.

6

Run the App

React Native enables simultaneous development of both an iOS and Android app that use the same React codebase. You can edit the .js or .ts source files in your project directory to develop your app.

In development, the apps read their React source code as a bundle from a local bundle server. To run the bundle server, use the following command in your React Native project directory:

npm start

With the bundle server running, you can now launch the Android and iOS apps:

  • To run the Android app, use Android Studio to open the android directory in your project directory and click Run.
  • To run the iOS app, use Xcode to open the .xcworkspace file in the ios directory. If you did not use CocoaPods during setup, open the .xcodeproj file in the ios directory instead. Once you have opened the project, click Run.

Import Realm

Add the following line to the top of your source files where you want to use MongoDB Realm:

import Realm from "realm";
import Realm from "realm";