expo-react-native-adapter
A React Native adapter for Expo Universal Modules. It requires expo-core
to be installed and linked.
Note: The following installation/setup instructions are only applicable to plain React Native applications, i. e. if your project is a detached Expo project and it has ExpoKit/expoview included, the installation has already been done for you.
JavaScript installation
$ yarn add expo-react-native-adapter # or $ npm install expo-react-native-adapter --save
Installation
iOS (Cocoapods)
If you're using Cocoapods, add the dependency to your Podfile
:
pod 'EXReactNativeAdapter', path: '../node_modules/expo-react-native-adapter/ios', inhibit_warnings: true
and run pod install
.
Android
- Append the following lines to
android/settings.gradle
:include ':expo-react-native-adapter'project(':expo-react-native-adapter').projectDir = new File(rootProject.projectDir, '../node_modules/expo-react-native-adapter/android') - Insert the following lines inside the dependencies block in
android/app/build.gradle
:compile project(':expo-react-native-adapter')
Additional required setup
iOS
-
Open the
AppDelegate.m
of your application. -
Import
<EXCore/EXModuleRegistry.h>
,<EXReactNativeAdapter/EXNativeModulesProxy.h>
and<EXReactNativeAdapter/EXModuleRegistryAdapter.h>
. -
Make
AppDelegate
implementRCTBridgeDelegate
protocol (@interface AppDelegate () <RCTBridgeDelegate>
). -
Add a new instance variable to your
AppDelegate
: -
In
-application:didFinishLaunchingWithOptions:
add the following at the top of the implementation:self.moduleRegistryAdapter = ; -
Add two methods to the
AppDelegate
's implementation:- (NSArray<> *)extraModulesForBridge:(RCTBridge *)bridge- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge -
When initializing
RCTBridge
, make theAppDelegate
a delegate of the bridge:RCTBridge *bridge = ; -
That's it! All in all, your
AppDelegate.m
should look similar to:Click to expand
Android
- Open the
MainApplication.java
of your application. - Add to the imports:
- Create an instance variable on the
Application
:private final ReactModuleRegistryProvider mModuleRegistryProvider = new ReactModuleRegistryProvider(Arrays.<Package>, /* singletonModules */ null); - Add
new ModuleRegistryAdapter(mModuleRegistryProvider)
to the list returned byprotected List<ReactPackage> getPackages()
. - You're good to go!
Usage
Calling methods on native modules
Native modules are available behind the proxy (NativeModulesProxy
of expo-core
).
To call an exported method, use NativeModulesProxy[clientCodeName].exportedMethod(...arguments)
, like this:
// For EX_REGISTER_MODULE(FileSystem,) or EX_REGISTER_EXPORTED_MODULE(FileSystem)// and EX_EXPORT_METHOD_AS(getInfo, getInfo:(NSString *)path) // or for method// @ExpoMethod// public void getInfo(String path, Promise promise)// defined in native module with name FileSystem ; const FileSystem = NativeModulesProxy; FileSystem;
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 expo-core
. The shared emitter emit events to react-native
's NativeEventEmitter
and expo-core
's EventEmitter
.
ExponentGyroscope.web.ts
// Example from expo-sensors native web gyroscope sensor ; SyntheticPlatformEmitter;
This emitted event is then received with a EventEmitter
in the developer-facing API.
; ; const nativeEmitter = ExponentGyroscope; // On Android and iOS, `nativeEmitter` receives events sent from Objective-C and Java. On web, it// receives events from the shared `SyntheticPlatformEmitter` instance.nativeEmitter;