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

@rnx-kit/react-native-host

v0.5.2

Published

Simplify React Native initialization

Downloads

51,416

Readme

@rnx-kit/react-native-host

Build npm version

@rnx-kit/react-native-host simplifies React Native initialization.

The aim of this package is to provide a backwards (and forwards) compatible way of initializing React Native, regardless of whether you're on New Architecture or have gone fully bridgeless.@rnx-kit/react-native-host will also a provide a simple way to enable split bundles and service delivery.

Installation

yarn add @rnx-kit/react-native-host --dev

or if you're using npm

npm add --save-dev @rnx-kit/react-native-host

Usage

iOS/macOS

[!NOTE]

For a working example how to use this library for iOS/macOS, see react-native-test-app.

To avoid accidental autolinking, @rnx-kit/react-native-host must be manually linked. In your Podfile, add the following line:

pod 'ReactNativeHost', :path => '../node_modules/@rnx-kit/react-native-host'

Adjust the path accordingly to your project setup. If you prefer a more dynamic approach, you can also try using this script:

proj_dir = Pathname.new(__dir__)
proj_dir = proj_dir.parent until
  File.exist?("#{proj_dir}/node_modules/@rnx-kit/react-native-host/ReactNativeHost.podspec") ||
  proj_dir.expand_path.to_s == '/'

pod 'ReactNativeHost', :path => "#{proj_dir}/node_modules/@rnx-kit/react-native-host"

Run pod install at least once to make sure it gets added to your project. Then make the following changes:

  • Replace instances of RCTBridgeDelegate with RNXHostConfig. The latter is a superset and is backwards compatible.
  • Replace instantiation of RCTBridge with ReactNativeHost. ReactNativeHost will instantiate the appropriate modules required for your setup. It will also handle New Architecture configuration as necessary.
  • Instead of instantiating RCTRootView directly, use -[ReactNativeHost viewWithModuleName:initialProperties:] to create your root views.

For example, if you previously had something like this:

// AppDelegate.h
@import React;
@import UIKit;

@interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate>
@end

// AppDelegate.m
@implementation AppDelegate {
    RCTBridge *_bridge;
}

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ...

    _bridge = [[RCTBridge alloc] initWithDelegate:self
                                    launchOptions:launchOptions];
    RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:_bridge
                                                     moduleName:moduleName
                                              initialProperties:initialProperties];

    ...
}

@end

You should instead have:

// AppDelegate.h
@import ReactNativeHost;
@import UIKit;

@interface AppDelegate : UIResponder <UIApplicationDelegate, RNXHostConfig>
@end

// AppDelegate.m
@implementation AppDelegate {
    ReactNativeHost *_host;
}

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ...

    _host = [[ReactNativeHost alloc] initWithConfig:self];
    UIView *rootView = [_host viewWithModuleName:moduleName
                               initialProperties:initialProperties];

    ...
}

@end

API

ReactNativeHost

Instantiates the appropriate modules required for the setup. It handles New Architecture if necessary.

initWithConfig:

Swift name: init(_:)

Creates an instance of ReactNativeHost using the designated initializer.

Objective-C:

ReactNativeHost *host = [[ReactNativeHost alloc] initWithConfig:self];

Swift:

let host = ReactNativeHost(config: self)

shutdown

Shuts down the React Native instance

usingModule:block:

Swift name: using(module:block:)

Retrieves or initializes a desired native module. Parameters:

  • moduleClass - class of the native module to initialize or retrieve
  • block - block that gets called when the native module is retrieved

Objective-C:

[host usingModule:[MyNativeModuleClass class] block:^(id<RCTBridgeModule> module) {
    if (![module isKindOfClass:[MyNativeModuleClass class]]) {
        return;
    }
    MyNativeModuleClass *myNativeModule = (MyNativeModuleClass *)module;
    // Use the native module here
}];

Swift:

host.using(module: MyNativeModuleClass.self) {
  guard let myNativeModule = module as? MyNativeModuleClass else {
    return
  }
  // Use the native module here
}

hostFromRootView:

Swift name: host(from:)

Retrieves the ReactNativeHost instance that view belongs to.

viewWithModuleName:initialProperties:

Swift name: view(moduleName:initialProperties:)

Creates a React root view with the specified module and initial properties. Parameters:

  • moduleName - name of the module to create root view of
  • initialProperties - properties passed to the module

Objective-C:

ReactNativeHost *host = [[ReactNativeHost alloc] initWithConfig:self];
UIView *rootView = [host viewWithModuleName:moduleName
                          initialProperties:initialProperties];

Swift:

let view = host.view(
    moduleName: moduleName,
    initialProperties: initialProperties
)

RNXConfig

RNXHostConfig is a superset of RCTBridgeDelegate and it's backwards compatible.

isDevLoadingViewEnabled

Returns whether the loading view should be visible while loading JavaScript

shouldReleaseBridgeWhenBackgrounded

Returns whether the bridge should be released when the app is in the background

onFatalError

Handles a fatal error