npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

expo-react-native-adapter

v3.0.1

Published

The adapter to use Expo universal modules with the React Native bridge

Downloads

6,731

Readme

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

  1. 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')
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:
    compile project(':expo-react-native-adapter')

Additional required setup

iOS

  1. Open the AppDelegate.m of your application.

  2. Import <EXCore/EXModuleRegistry.h>, <EXReactNativeAdapter/EXNativeModulesProxy.h> and <EXReactNativeAdapter/EXModuleRegistryAdapter.h>.

  3. Make AppDelegate implement RCTBridgeDelegate protocol (@interface AppDelegate () <RCTBridgeDelegate>).

  4. Add a new instance variable to your AppDelegate:

    @interface AppDelegate () <RCTBridgeDelegate>
    
    // add this line
    @property (nonatomic, strong) EXModuleRegistryAdapter *moduleRegistryAdapter;
    
    @end
  5. In -application:didFinishLaunchingWithOptions: add the following at the top of the implementation:

    self.moduleRegistryAdapter = [[EXModuleRegistryAdapter alloc] initWithModuleRegistryProvider:[[EXModuleRegistryProvider alloc] init]];
  6. Add two methods to the AppDelegate's implementation:

    - (NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge
    {
        NSArray<id<RCTBridgeModule>> *extraModules = [_moduleRegistryAdapter extraModulesForBridge:bridge andExperience:nil];
        // If you'd like to export some custom RCTBridgeModules that are not Expo modules, add them here!
        return extraModules;
    }
    
    - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
        return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
    }
  7. When initializing RCTBridge, make the AppDelegate a delegate of the bridge:

    RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  8. That's it! All in all, your AppDelegate.m should look similar to:

    #import "AppDelegate.h"
    
    #import <React/RCTBundleURLProvider.h>
    #import <React/RCTRootView.h>
    
    #import <EXCore/EXModuleRegistry.h>
    #import <EXReactNativeAdapter/EXNativeModulesProxy.h>
    #import <EXReactNativeAdapter/EXModuleRegistryAdapter.h>
    
    @interface AppDelegate () <RCTBridgeDelegate>
    
    @property (nonatomic, strong) EXModuleRegistryAdapter *moduleRegistryAdapter;
    
    @end
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.moduleRegistryAdapter = [[EXModuleRegistryAdapter alloc] initWithModuleRegistryProvider:[[EXModuleRegistryProvider 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 andExperience:nil];
        // If you'd like to export some custom RCTBridgeModules that are not Expo modules, add them here!
        return extraModules;
    }
    
    - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
        return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
    }
    
    @end

Android

  1. Open the MainApplication.java of your application.
  2. Add to the imports:
    import expo.adapters.react.ModuleRegistryAdapter;
    import expo.adapters.react.ReactAdapterPackage;
    import expo.adapters.react.ReactModuleRegistryProvider;
    import expo.core.interfaces.Package;
  3. Create an instance variable on the Application:
    private final ReactModuleRegistryProvider mModuleRegistryProvider = new ReactModuleRegistryProvider(Arrays.<Package>asList(
        new ReactAdapterPackage()
        // more packages, like
        // new CameraPackage(), if you use expo-camera
        // etc.
    ), /* singletonModules */ null);
  4. Add new ModuleRegistryAdapter(mModuleRegistryProvider) to the list returned by protected List<ReactPackage> getPackages().
  5. 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

import { NativeModulesProxy } from 'expo-core';

const { FileSystem } = NativeModulesProxy;

FileSystem.getInfo('file:///...');

Note that all the methods return Promises.

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

import { SyntheticPlatformEmitter } from 'expo-core';

SyntheticPlatformEmitter.emit('gyroscopeDidUpdate', { x, y, z });

This emitted event is then received with a EventEmitter in the developer-facing API.

import { EventEmitter } from 'expo-core';

import ExponentGyroscope from './ExponentGyroscope';

const nativeEmitter = new EventEmitter(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.addListener('gyroscopeDidUpdate', ({ x, y, z }) => {});