Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
Click here >
Docs Menu
Docs Home
/ /
SDK de Dispositivo Atlas

Install the React Native SDK

El Atlas Device SDK para React Native permite el desarrollo de React NativeAplicaciones que utilizan JavaScript y TypeScript. React Native permite crear aplicaciones multiplataforma para iOS y Android con una única base de código utilizando el framework React.

Antes de comenzar, asegúrese de que su entorno de desarrollo cumpla con los siguientes requisitos previos. Estas son necesarias para la última versión del SDK de React Native:

  • Follow the official React Native CLI Quickstart instructions to set up your environment.

  • React Native v0.71.4 or later. Check out the compatibility chart to determine which version of React Native is compatible with specific React Native SDK versions.

Nota

Realm JS v10.6.0 and Later Support Mac Catalyst

You can use the React Native SDK with a bare React Native app or Expo. This page and the React Native SDK documentation generally assume that you're using a bare React Native app and not Expo.

Si quieres utilizar el SDK de React Native con Expo, consulta la página Bootstrap con Expo.

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

Tip

Atlas Device SDK and Realm

The SDK uses Realm Core database for device data persistence. When you install the React Native SDK, the package names reflect Realm naming.

1

Create your React Native project with the following command:

npx react-native init MyApp

Cambie al directorio del proyecto que el comando anterior acaba de crear:

cd MyApp
2

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

npm install realm@12
3

Nota

To use Hermes, your app must use Realm v11 or later and React Native 0.70.0 or later

The SDK supports React Native's mobile-optimized JavaScript engine, Hermes. By default, new apps created with the React Native CLI already have Hermes enabled.

Recomendamos usar Hermes con el SDK. Sin embargo, el SDK también es compatible con el motor JavaScriptCore (JSC) si tu aplicación lo requiere.

Existing apps that currently use JSC can enable Hermes separately for Android and iOS. To learn how, see the Using Hermes guide in the React Native docs.

4

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

cd ios && pod install && cd ..

Esto descarga las bibliotecas SDK y regenera el proyecto. .xcworkspace archivo con el que puedes trabajar en Xcode para ejecutar tu aplicación.

5

You may need to extend your Proguard configuration to use it with an Android app. In your Proguard configuration, add the following:

-keep class io.realm.react.**
6

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. The SDK supports TypeScript natively and integrates easily into a TypeScript project.

7

@realm/react es un paquete npm que agiliza operaciones comunes del SDK como consultar, escribir en una base de datos y escuchar notificaciones de cambios de objetos. Esto reduce el código redundante, como crear tus propios escuchadores y la gestión de estado.

@realm/react provides access to the SDK through a set of providers that have various hooks. The hooks update React state when the data changes. This means that components using these hooks rerender on any changes to data in the database.

Nota

Use @realm/react with Realm JS Version 11 or later

To use Realm JS version 11.0.0 or later with @realm/react, you must use @realm/react version 0.4.0 or later.

In your React Native project directory, add @realm/react to your project with the following command:

npm install @realm/react
8

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

Con el servidor bundle en funcionamiento, ahora puedes lanzar las aplicaciones Android e iOS:

  • Para ejecutar la aplicación de Android, use Android Studio para abrir el directorio android en el directorio de su proyecto y haga clic en 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.

Nota

Requisito de versión de @realm/react

The @realm/react library requires react-native version >= 0.59. If you are developing using older versions of React Native, you can use the SDK without @realm/react. Since the React Native SDK documentation makes heavy use of the @realm/react package, you may want to refer to the Node.js SDK documentation.

1

Create your React Native project with the following command:

react-native init MyApp

Cambie al directorio del proyecto que el comando anterior acaba de crear:

cd MyApp
2

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

npm install realm@12
3

En el directorio de tu Proyecto React Native, vincula el módulo nativo del SDK a tu Proyecto con el siguiente comando:

react-native link realm
4

Para el desarrollo en Android, confirma que el paso de vinculación agregó correctamente el módulo del SDK a los archivos de configuración de Gradle. En algunas versiones, se ha observado que React Native no vincula correctamente el SDK. Si esto ocurre, puedes vincular el SDK manualmente agregando las líneas que falten a los archivos correspondientes.

Primero, asegúrese de que el archivo android/settings.gradle incluya el SDK y especifique el directorio del proyecto del SDK:

rootProject.name = 'MyApp'
include ':realm'
project(':realm').projectDir = new File(rootProject.projectDir, '../node_modules/realm/android')
apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)
include ':app'

Second, ensure the android/app/build.gradle file's dependencies section declares the SDK as a dependency:

dependencies {
implementation project(':realm')
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.facebook.react:react-native:+" // From node_modules
// ...
}

Por último, asegúrese de que el archivo MainApplication.java importe el RealmReactPackage y lo instancia en su lista de paquetes.

import io.realm.react.RealmReactPackage; // Add this import.
public class MainApplication extends Application implements ReactApplication {
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new RealmReactPackage() // Add this line.
);
}
// ...
}
5

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. The SDK supports TypeScript natively and integrates easily into a TypeScript project.

6

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

Con el servidor bundle en funcionamiento, ahora puedes lanzar las aplicaciones Android e iOS:

  • 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.

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

import Realm from "realm";

Next

Bienvenido a la Docs de Atlas Device SDK

En esta página