Top Banner
REACT NATIVE FOR REACT DEVELOPERS Barak Cohen, Wondermall
44

React Native for ReactJS Devs

Jan 08, 2017

Download

Software

Barak Cohen
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: React Native for ReactJS Devs

REACT NATIVE FOR REACT DEVELOPERSBarak Cohen, Wondermall

Page 2: React Native for ReactJS Devs
Page 3: React Native for ReactJS Devs

FRONT-END IS BROKEN• Build the same app 3-6 times: iOS

(Phone/Tablet), Android (Phone/Tablet), Web (Desktop/Phone)

• Different teams build the same thing using 3 different languages, frameworks & libraries.

• Hard to maintain feature parity & consistent terminology.

• Impossible to be an expert in all technologies and “own” something in all products

Page 4: React Native for ReactJS Devs

STACK-CENTRIC R&D

iOS

Product Manager

Web Android

iOS Dev

iOS Dev

iOS Dev

Web Dev

Web Dev

Web Dev

Android Dev

Android Dev

Android Dev

Page 5: React Native for ReactJS Devs

BUSINESS-CENTRIC R&D

Feature A

Product Manager

Feature B Feature CFull-Stack

DevFull-Stack

DevFull-Stack

Dev

Full-Stack Dev

Full-Stack Dev

Full-Stack Dev

Full-Stack Dev

Full-Stack Dev

Full-Stack Dev

Page 6: React Native for ReactJS Devs

FULL-STACK IS HARD!• Almost all devs at Wondermall do Python,

ObjC+Swift and AngularJS

• Level of expertise vary. No one is a rock star on all 3 platforms

• We’ll never get everyone cross trained on Android, Windows Phone, Apple Watch, Apple TV, …

• If we want to remain Full Stack, we’ll have to rethink our strategy

Page 7: React Native for ReactJS Devs

Powered by React-Native

Page 8: React Native for ReactJS Devs

BASICS

Page 9: React Native for ReactJS Devs

REACT-NATIVE IS…• JavaScript for Application Logic

• Native UI (No WebViews)

• Flexbox Layout

• Polyfills (fetch API) & Platform APIs (Camera)

• Compatible with nearly all React and JS libs

Page 10: React Native for ReactJS Devs

GETTING STARTED

$ brew install node && brew install watchman

$ npm install -g react-native-cli

$ react-native init MyProject && cd MyProject

$ react-native run-ios

Page 11: React Native for ReactJS Devs

HELLO WORLDvar React = require('react')var { View, Text } = require('react-native')

class HelloWorldView extends React.Component { render() { return ( <View><Text>Hello World</Text></View> ) }}

Page 13: React Native for ReactJS Devs

REACT (NATIVE+JS) BENEFITS

• “Learn once write anywhere”

• Reuse almost all non-UI code across all platforms

• Reuse most UI code between Native Platforms

• Hot-reloading JS, debug in Chrome

Page 14: React Native for ReactJS Devs

BENEFITS - CON’T• Can be added incrementally to an

existing app

• Over-the-wire updates w/o AppStore (AppHub)

• JS w/ ES6 is succinct and productive

• Can use previously written native code and UI

Page 15: React Native for ReactJS Devs

STYLE & LAYOUT

Page 16: React Native for ReactJS Devs

ADDING STYLEclass HelloWorldView extends React.Component { ...

render() { return ( <View style={{padding: 10}}> <Text style={{fontSize: 14, color: '#0000ff'}}> Hello World </Text> </View> ) }}

Page 17: React Native for ReactJS Devs

FLEXBOX LAYOUTclass HelloWorldView extends React.Component { render() { return ( <View style={{flexDirection: 'column', flex: 1}}> <View style={{flex: 1, backgroundColor: 'red'}}></View> <View style={{flex: 1, backgroundColor: 'green'}}></View> <View style={{flex: 1, backgroundColor: 'blue'}}></View> </View> ) }}

Page 18: React Native for ReactJS Devs

FLEXBOX LAYOUT - 2class HelloWorldView extends React.Component { render() { return ( <View style={{flexDirection: 'row', flex: 1}}> <View style={{flex: 1, backgroundColor: 'red'}}></View> <View style={{flex: 1, backgroundColor: 'green'}}></View> <View style={{flex: 1, backgroundColor: 'blue'}}></View> </View> ) }}

Page 19: React Native for ReactJS Devs

NATIVE APISimport React, { Component } from 'react';import { MapView } from 'react-native';

class MapMyRide extends Component { render() { return ( <MapView style={{height: 200, margin: 40}} showsUserLocation={true} /> ); }}

Page 20: React Native for ReactJS Devs

ROUTING

Page 21: React Native for ReactJS Devs

return ( <Navigator initialRoute={{ title: 'My Initial Scene', index: 0 }} renderScene={(route, navigator) => <MyScene title={route.title} onForward={ () => { navigator.push({ title: 'Scene ' + route.index + 1, index: route.index + 1, }); }} /> } /> )

BUILT-IN

Page 22: React Native for ReactJS Devs

REACT-NATIVE-ROUTER-FLUX

import { PageOne, PageTwo } from './Pages';

export default class App extends Component { render() { return ( <Router> <Scene key="root"> <Scene key="pageOne" component={PageOne} title="PageOne" initial={true} /> <Scene key="pageTwo" component={PageTwo} title="PageTwo" /> </Scene> </Router> ) }}

Page 23: React Native for ReactJS Devs

UNIVERSAL APPS

Page 24: React Native for ReactJS Devs

Shared - Native & WebRedux / Relay

API Client

Business Logic

Shared Native

Native Specific<TabBarIOS>,

<AndroidToolbar>

<Text>, <TextInput>, <Image>

Web<div>

Business Level Comp’

<span>

<img>

Page 25: React Native for ReactJS Devs

DEBUGGING & TOOLING

Page 26: React Native for ReactJS Devs

• Reload - Clears all JS code and resets the state

• Remote JS Debugging - Chrome / VSCode with breakpoints and Console (no network)

• Live Reload - Automatic Reload (as above)

• Hot Reloading - Change JS code without resetting the state

DEVELOPER MENU

Page 27: React Native for ReactJS Devs

REDUX REMOTE DEV TOOLS

Page 28: React Native for ReactJS Devs

PACKAGER• Built-in packager with ES6 support

• Development: local webserver serves the bundled JS

• Production:

• Use .ios.js or .android.js suffixes, Packager will include the correct file for the platform bundle

$ react-native bundle —platform ios

Page 29: React Native for ReactJS Devs

OTA UPDATES• Updates JS & assets without AppStore

submission

• Apple’s policy allows it for fixes & minor improvements

• Microsoft CodePush - Commercial

• AppHub - Hosted / OSS

Page 30: React Native for ReactJS Devs

INTEGRATION & INTERNALS

Page 31: React Native for ReactJS Devs

NATIVE VS. JAVASCRIPTView ControllerView ControllerView Controller Bridge

(incl. JS VM)

JavaScript FileJavaScript FileJavaScript File

RCTRootViewUIView

UIView

Page 32: React Native for ReactJS Devs

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions…{ NSURL *jsCodeLocation = @“http://localhost:8081/index.ios.bundle?platform=ios";

RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName:@"MyComponent" initialProperties:@{} launchOptions:launchOptions];

UIViewController *rootViewController = [[UIViewController alloc] init]; rootViewController.view = rootView;

self.window.rootViewController = rootViewController; [self.window makeKeyAndVisible];}

Page 33: React Native for ReactJS Devs

MULTIPLE RCTROOTVIEWS

RCTRootViewUIView

UIView

RCTRootViewUIView

UIView

Bridge(incl. JS VM)

Page 34: React Native for ReactJS Devs

{ // AppDelegate didFinishLaunchingWithOptions self.bridge = [[RCTBridge alloc] initWithBundleURL:@"localhost..." moduleProvider:nil launchOptions:nil]

// When you want to show R/N view [[RCTRootView alloc] initWithBridge:self.bridge moduleName:"MyComponent"];

// Somewhere else [[RCTRootView alloc] initWithBridge:self.bridge moduleName:"MyOtherComponent"];

}

Page 35: React Native for ReactJS Devs

NATIVE MODULES• Export native functions to be called

from JS

• Export constants and enums

• Send events to JS from native

• They are singletons

Page 36: React Native for ReactJS Devs

// CalendarManager.h#import "RCTBridgeModule.h"

@interface CalendarManager : NSObject <RCTBridgeModule>@end

// CalendarManager.m@implementation CalendarManager

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(addEvent:(NSString *)name location:(NSString *)location){ RCTLogInfo(@“Creating event %@ at %@", name, location);}

@end

Page 37: React Native for ReactJS Devs

RETURN VALUE W/ PROMISES

RCT_EXPORT_METHOD(findEvents, resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){ NSArray *events = ... if (events) { resolve(events); } else { NSError *error = ... reject(@"no_events", @"There were no events", error); }}

Page 38: React Native for ReactJS Devs

import { NativeModules } from ‘react-native';

CalendarManager = NativeModules.CalendarManager;

CalendarManager.addEvent(‘Birthday Party’, …);

async function updateEvents() {try { var events = await CalendarManager.findEvents();

this.setState({ events });} catch (e) { console.error(e);}

}

updateEvents();

Page 39: React Native for ReactJS Devs

MISC.

Page 40: React Native for ReactJS Devs

PERFORMANCE TIPS• Minimize bridge messages

• Implement shouldComponentUpdate

• Direct Manipulation with setNativeProps

• Rewrite in Native

Page 41: React Native for ReactJS Devs

FURTHER READING• Facebook’s F8 App -

http://makeitopen.com/

• React Native Newsletter - http://reactnative.cc/

• Practice layout and Flexbox https://github.com/jondot/ReactNativeKatas

• https://github.com/jondot/awesome-react-native

Page 42: React Native for ReactJS Devs

STARTER KITS

• Native Starter - http://startreact.com/themes/native-starter/

• Ignite - https://github.com/infinitered/ignite

• este.js - https://github.com/este/este

Page 43: React Native for ReactJS Devs

3RD-PARTY COMPONENTS

• https://js.coach/react-native/

• https://react.parts/native

• nativebase.io

Page 44: React Native for ReactJS Devs

THANK YOU!(QUESTIONS?)