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.
Requisitos previos
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 y versiones posteriores son compatibles con Mac Catalyst
For React Native version 0.64 and below, you must take additional steps to build your application when using Mac Catalyst.
Use the SDK with Expo
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.
Instala el SDK en una aplicación React Native bare
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.
Enable Hermes
Nota
Para usar Hermes, su aplicación debe usar Realm v11 o posterior y React Native 0.70.0 o posterior
El SDK es compatible con Hermes, el motor de JavaScript optimizado para dispositivos móviles de React Native. De forma predeterminada, las nuevas aplicaciones creadas con la CLI de React Native ya tienen Hermes habilitado.
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.
Resolver dependencias de CocoaPods
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.
Enable TypeScript (recommended, but 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. The SDK supports TypeScript natively and integrates easily into a TypeScript project.
Instalar la biblioteca @realm/react
@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
Utilice @realm/react con Realm JS versión 11 o posterior
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
Ejecutar la aplicación
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
androiden el directorio de su proyecto y haga clic en Run.To run the iOS app, use Xcode to open the
.xcworkspacefile in theiosdirectory. If you did not use CocoaPods during setup, open the.xcodeprojfile in theiosdirectory 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.
Confirm the Link Step (Android)
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 { protected List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( new MainReactPackage(), new RealmReactPackage() // Add this line. ); } // ... }
Habilite TypeScript (opcional)
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.
Ejecutar la aplicación
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
androiddirectory in your project directory and click Run.To run the iOS app, use Xcode to open the
.xcworkspacefile in theiosdirectory. If you did not use CocoaPods during setup, open the.xcodeprojfile in theiosdirectory instead. Once you have opened the project, click Run.
Import the SDK
Add the following line to the top of your source files where you want to use the SDK:
import Realm from "realm";