Install Realm for React Native¶
Before getting started, ensure your development environment meets the following prerequisites:
- React Native v0.31.0 or later. Follow the official React Native CLI Quickstart instructions to set up your environment.
- CocoaPods (recommended for building an iOS app with React Native v.60+).
Unfortunately, Expo does not support Realm.
If you use the Expo CLI or
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.
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.
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:
Install Realm with NPM¶
In your React Native project directory, add Realm to your project with the following command:
npm install realm
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
.xcworkspace file that you can work with in
Xcode to run your application.
Enable TypeScript (optional)¶
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
.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:
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
androiddirectory in your project directory and click Run.
- To run the iOS app, use Xcode to open the
.xcworkspacefile in the
iosdirectory. If you did not use CocoaPods during setup, open the
.xcodeprojfile in the
iosdirectory instead. Once you have opened the project, click Run.
Add the following line to the top of your source files where you want to use MongoDB Realm:
import Realm from "realm";