What is @unimodules/react-native-adapter?
@unimodules/react-native-adapter is a package that provides a bridge between React Native and the Expo unimodules. It allows you to use Expo modules in a bare React Native project, enabling functionalities such as accessing device sensors, file system, and more.
What are @unimodules/react-native-adapter's main functionalities?
Accessing Device Sensors
This feature allows you to access device sensors like the accelerometer. The code sample demonstrates how to import the Accelerometer module and add a listener to receive accelerometer data.
import { Accelerometer } from '@unimodules/react-native-adapter';
Accelerometer.addListener(accelerometerData => {
console.log(accelerometerData);
});
File System Access
This feature allows you to interact with the device's file system. The code sample shows how to read a file from the document directory.
import { FileSystem } from '@unimodules/react-native-adapter';
FileSystem.readAsStringAsync(FileSystem.documentDirectory + 'example.txt')
.then(content => {
console.log(content);
});
Notifications
This feature allows you to schedule local notifications. The code sample demonstrates how to schedule a notification to be triggered after 1 second.
import { Notifications } from '@unimodules/react-native-adapter';
Notifications.scheduleLocalNotificationAsync({
title: 'Hello',
body: 'World',
}, {
time: new Date().getTime() + 1000
});
Other packages similar to @unimodules/react-native-adapter
react-native-sensors
react-native-sensors provides access to device sensors like accelerometer, gyroscope, and magnetometer. It is similar to @unimodules/react-native-adapter in terms of accessing device sensors but does not offer the broader range of Expo modules.
react-native-fs
react-native-fs is a file system access library for React Native. It offers similar file system functionalities as @unimodules/react-native-adapter but is focused solely on file system operations.
react-native-push-notification
react-native-push-notification is a library for handling local and remote notifications in React Native. It provides similar notification functionalities as @unimodules/react-native-adapter but is specialized in notifications.
@unimodules/react-native-adapter
A React Native adapter for Expo Universal Modules. It requires @unimodules/core
to be installed and linked.
JavaScript installation
$ yarn add @unimodules/react-native-adapter
$ npm install @unimodules/react-native-adapter --save
Installation
If you are using react-native-unimodules
, this package will already be installed and configured!
iOS (Cocoapods)
If you're using Cocoapods, add the dependency to your Podfile
:
pod 'UMReactNativeAdapter', path: '../node_modules/@unimodules/react-native-adapter/ios', inhibit_warnings: true
and run npx pod-install
.
Android
- Append the following lines to
android/settings.gradle
:
include ':unimodules-react-native-adapter'
project(':unimodules-react-native-adapter').projectDir = new File(rootProject.projectDir, '../node_modules/@unimodules/react-native-adapter/android')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:
compile project(':unimodules-react-native-adapter')
Additional required setup
iOS
-
Open the AppDelegate.m
of your application.
-
Import <UMCore/UMModuleRegistry.h>
, <UMReactNativeAdapter/UMNativeModulesProxy.h>
and <UMReactNativeAdapter/UMModuleRegistryAdapter.h>
.
-
Make AppDelegate
implement RCTBridgeDelegate
protocol (@interface AppDelegate () <RCTBridgeDelegate>
).
-
Add a new instance variable to your AppDelegate
:
@interface AppDelegate () <RCTBridgeDelegate>
@property (nonatomic, strong) UMModuleRegistryAdapter *moduleRegistryAdapter;
@end
-
In -application:didFinishLaunchingWithOptions:
add the following at the top of the implementation:
self.moduleRegistryAdapter = [[UMModuleRegistryAdapter alloc] initWithModuleRegistryProvider:[[UMModuleRegistryProvider alloc] init]];
-
Add two methods to the AppDelegate
's implementation:
- (NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge
{
NSArray<id<RCTBridgeModule>> *extraModules = [_moduleRegistryAdapter extraModulesForBridge:bridge];
return extraModules;
}
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
}
-
When initializing RCTBridge
, make the AppDelegate
a delegate of the bridge:
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
or, if you use react-native-navigation
, add the bridgeManagerDelegate
parameter of self
, like:
-[ReactNativeNavigation bootstrap:jsCodeLocation launchOptions:launchOptions];
+[ReactNativeNavigation bootstrap:jsCodeLocation launchOptions:launchOptions bridgeManagerDelegate:self];
-
That's it! All in all, your AppDelegate.m
should look similar to:
Click to expand
#import "AppDelegate.h"
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <UMCore/UMModuleRegistry.h>
#import <UMReactNativeAdapter/UMNativeModulesProxy.h>
#import <UMReactNativeAdapter/UMModuleRegistryAdapter.h>
@interface AppDelegate () <RCTBridgeDelegate>
@property (nonatomic, strong) UMModuleRegistryAdapter *moduleRegistryAdapter;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.moduleRegistryAdapter = [[UMModuleRegistryAdapter alloc] initWithModuleRegistryProvider:[[UMModuleRegistryProvider alloc] init]];
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"YOUR_MODULE_NAME" initialProperties:nil];
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
return YES;
}
- (NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge
{
NSArray<id<RCTBridgeModule>> *extraModules = [_moduleRegistryAdapter extraModulesForBridge:bridge];
return extraModules;
}
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
}
@end
Android
- Open the
MainApplication.java
of your application. - Add to the imports:
import org.unimodules.adapters.react.ModuleRegistryAdapter;
import org.unimodules.adapters.react.ReactAdapterPackage;
import org.unimodules.adapters.react.ReactModuleRegistryProvider;
import org.unimodules.core.interfaces.Package;
- Create an instance variable on the
Application
:
private final ReactModuleRegistryProvider mModuleRegistryProvider = new ReactModuleRegistryProvider(Arrays.<Package>asList(
new ReactAdapterPackage()
), null);
- Add
new ModuleRegistryAdapter(mModuleRegistryProvider)
to the list returned by protected List<ReactPackage> getPackages()
. - You're good to go!
Usage
Calling methods on native modules
Native modules are available behind the proxy (NativeModulesProxy
of @unimodules/core
).
To call an exported method, use NativeModulesProxy[clientCodeName].exportedMethod(...arguments)
, like this:
import { NativeModulesProxy } from '@unimodules/core';
const { FileSystem } = NativeModulesProxy;
FileSystem.getInfo('file:///...');
Note that all the methods return Promise
s.
Synthetic Platform Events
When creating web universal modules, you may find that you need to send events back to the API layer.
In this case you will want to use the shared SyntheticPlatformEmitter
instance from @unimodules/core
. The shared emitter emit events to react-native
's NativeEventEmitter
and @unimodules/core
's EventEmitter
.
ExponentGyroscope.web.ts
import { SyntheticPlatformEmitter } from '@unimodules/core';
SyntheticPlatformEmitter.emit('gyroscopeDidUpdate', { x, y, z });
This emitted event is then received with a EventEmitter
in the developer-facing API.
import { EventEmitter } from '@unimodules/core';
import ExponentGyroscope from './ExponentGyroscope';
const nativeEmitter = new EventEmitter(ExponentGyroscope);
nativeEmitter.addListener('gyroscopeDidUpdate', ({ x, y, z }) => {});