Code Monkey home page Code Monkey logo

react-native-hybrid-app-examples's Introduction

React Native Hybrid App Examples

A collection of examples for using React Native in an existing iOS application

Concepts

Pre-loading the Bridge

One of the first things that you should do, if you want decent performance out of your hybrid app, is to pre-load your RCTBridge and keep a reference of it somewhere (possibly your AppDelegate):

AppDelegate.h

@property (nonatomic, strong) RCTBridge *bridge;

AppDelegate.m

@synthesize bridge;
...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ...configure the jsCodeLocation here...

    bridge = [[RCTBridge alloc] initWithBundleURL:jsCodeLocation
                                   moduleProvider:nil
                                    launchOptions:launchOptions];
                                    
    ...setup your rootViewController here...
    
    return YES;
}

This will allow the JavaScript to pre-load and allow you to use this bridge later on in other parts of the app:

AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:delegate.bridge moduleName:@"MyModule"];

Multiple "Entry" Points

In your index.ios.js file, you can "register" different modules that you want to use as seperate entry points in your app:

var React = require('react-native');
var {
  AppRegistry,
} = React;

var MainEntry = require('./Main');
var SecondEntry = require('./Second');
var ThirdEntry = require('./Third');

AppRegistry.registerComponent('MainEntry', () => MainEntry);
AppRegistry.registerComponent('SecondEntry', () => SecondEntry);
AppRegistry.registerComponent('ThirdEntry', () => ThirdEntry);

You can then pass these in as the moduleName when creating a RCTRootView:

RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:delegate.bridge moduleName:@"SecondEntry"];

Examples

Simple

  1. React View inside a Native View

    • Very basic example that shows how a React Native component can be loaded inside of a UIView that already exists in UIViewController.
  2. Passing Data into a React View

    • Demonstrates how you can use initialProperties on RCTRootView to pass data to your React Native component.

Advanced

  1. Native Modal With RN Navigation
    • This example demonstrates how a React Native component can communicate & perform actions on it's native Obj-C container.
    • Using a Coordinator native module, we are able to delegate method calls to the native Obj-C container.
      • Why don't we just export the methods directly on the Obj-C container itself? When a bridge is initialized, each native module gets instantiated and the bridge holds that instance. If we were to create another instance of that Obj-C container (from within our base Obj-C app), the JS calls wouldn't be able to use that instance, since it only knows about the instance on the bridge.
        • For this reason, we pass in a delegate to the native module instance on the bridge after the RCTBridge and RCTRootView are created.

        • For example:

          [(MyNativeModule *)[myAppdelegate.bridge.modules[@"MyNativeModule"] setDelegate:self]];
      • Why don't we use the moduleProvider block to pass in instances of the modules when the bridge loads? Since, we are trying to load the bridge at startup, for performance gains, we don't yet have the instance of all of our view controllers.
        • It's possible to do this (and perhaps recommended - no need for a Coordinator), but you would need to create your bridges when your container view controller loads, this could cause performance issues if you want your React Native components to display immediately.

        • For example (when loading your container view controller):

          RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:jsCodeLocation 
                                                moduleProvider:^ NSArray *{
                                                  return @[self];
                                                }
                                                 launchOptions:nil];
          
          RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName: @"MyModule"];
        • There is more discussion about this technique here.

Discussions About Hybrid Apps

Questions? Suggestions?

  • Are there a specific use-cases that you need a solution for? Is the documentation here a little confusing? Feel free to open up an issue.
  • Have you implemented something else than what's listed here? Can these examples be implemented in a better way? Please submit a pull request!

react-native-hybrid-app-examples's People

Contributors

dsibiski avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.