Upgrade to Pro — share decks privately, control downloads, hide ads and more …

React Native for Android

React Native for Android

Right now, as a mobile developer you must choose between your own efficiency and providing the user with the best native experience. React Native aims to give you the power to develop these great experiences with web-like velocity. React, a powerful js framework that rethinks UI development, can now be used to write real mobile apps for Android and iOS. This talk will explore why React Native matters for Android developers.

Olivia Bishop

October 30, 2015
Tweet

Other Decks in Programming

Transcript

  1. What is React Native for? Why it might be useful

    to an Android developer How it works Agenda React basics
  2. Native apps can be great experiences • Snappy gestures and

    animations • Look good on Android • Integrate well with the OS
  3. But they come with challenges 1. Slow to iterate (build

    & install) 2. Hard to learn (different environments for each platform) 3. Imperative APIs
  4. SOLUTIONS CHALLENGES INSTANT RELOAD BUILD & INSTALL 
 ON EVERY

    CHANGE 1 DECLARATIVE APIS IMPERATIVE APIS 3 SINGLE ENVIRONMENT FOR MULTIPLE PLATFORMS DIFFERENT ENVIRONMENTS FOR EACH PLATFORM 2
  5. if (count > 99) { if (!hasFire()) {
 addFire();
 }

    } else { if (hasFire()) {
 removeFire();
 }
 } if (count === 0) { if (hasBadge()) {
 removeBadge();
 } return;
 } if (!hasBadge()) {
 addBadge();
 } var countText = count > 99 ? '99+' : count.toString(); getBadge().setText(countText); Imperative
  6. 8 99+ 8 99+ 8 8 8 8 99+ 99+

    99+ 99+ 3 states 9 transitions
  7. Declarative if (count === 0) { return <div class="bell"></div>; 8

    } else if(count <= 99) { return ( <div class="bell"> <badge count={count} /> </div> ); 99+ } else { return ( <div class="bell onFire”> <badge count=“99+” /> </div> ); }
  8. var Toggle = React.createClass({ getInitialState: function() { return {on: false};

    }, handlePress: function(event) { this.setState({on: !this.state.on}); }, render: function() { var text = this.state.on ? 'Toggle on' : 'Toggle off'; return ( <TouchableWithoutFeedback onPress={this.handlePress}> <View> <Text> {text} </Text> </View> </TouchableWithoutFeedback> ); } });
  9. Architecture • A set of native modules (java) and a

    set of JS modules call methods on each other over a bridge • module: an object with some state and a collection of methods that can be called • bridge: the glue that facilitates these calls back and forth
  10. • Async • Batched • Serialized method calls Native sends

    a single JS call… …JS executes it and responds with a list of native calls Native JS Executor JSC via C++ bindings (shipped in APK) Java/C++ via JNI BRIDGE (C++/Java)
  11. Serialized communication 1. UIManager   1. createView   2. updateProperties

      2. Networking   1. sendRequest   3. Storage   1. get   2. set   3. delete UIManager.createView(‘Text’,  {‘foo’:  ‘bar’});   [1,  1,  [‘Text’,  {‘foo’:  ‘bar’}]] Storage.set(‘key’,  ‘value’);   [3,  2,  [‘key’,  ‘value’]]
  12. AppRegistry.runApplication(   ‘MyApp’,   new  WritableMap()); require(‘AppRegistry’).runApplication(‘MyApp’,  {}); UIManager.createView(2,  ’View’,

     {…}); UIManager.createView(3,  ’Text’,  {…}); UIManager.createView(4,  ’View’,  {…}); … UIManagerModule#createView(2,  ‘View’,  …)   View  newView  =  new  View();   UIManagerModule#createView(3,  ‘Text’,  …)   TextView  newView  =  new  TextView();   UIManagerModule#createView(4,  ‘View’,  …)   View  newView  =  new  View();   … BRIDGE [1, 1, [‘MyApp’, {}]] [[2, 3, [2, ‘View’, {…}]], [2, 3, [3, ‘Text’, {…}]], [2, 3, [4, ‘View’, {…}]]] Java JS
  13. Threading Model • Three threads: UI (app main thread), native

    modules, JS • Each runs an Android Looper (event loop) and processes a queue of Runnable events • Runnables execute within an error boundary so that native Exceptions redbox instead of hard crashing
  14. Handle Timer Handle Timer Timer Fires Timer Fires UI Event

    Queue Native Modules Event Queue JS Event Queue Unrelated JS Operation 1 Unrelated JS Operation 2 Unrelated Native Operation 1 Unrelated Native Operation 2 Unrelated Native Operation 3 Dispatch View Updates Update UI Unrelated Native Operation 1 Unrelated Native Operation 2 Unrelated Native Operation 3 Unrelated JS Operation 1 Unrelated JS Operation 2 Dispatch View Updates Update UI
  15. public class ToastModule extends ReactContextBaseJavaModule { public ToastModule(ReactApplicationContext reactContext) {

    super(reactContext); } @Override public String getName() { return "ToastAndroid"; } }
  16. public class ToastModule extends ReactContextBaseJavaModule { public ToastModule(ReactApplicationContext reactContext) {

    super(reactContext); } @Override public String getName() { return "ToastAndroid"; } @ReactMethod public void show(String message, int duration) { Toast.makeText(getReactApplicationContext(), message, duration).show(); } }
  17. public class ToastModule extends ReactContextBaseJavaModule { private static final String

    DURATION_SHORT_KEY = "SHORT"; private static final String DURATION_LONG_KEY = "LONG"; public ToastModule(ReactApplicationContext reactContext) { super(reactContext); } @Override public String getName() { return "ToastAndroid"; } @Override public Map<String, Object> getConstants() { final Map<String, Object> constants = MapBuilder.newHashMap(); constants.put(DURATION_SHORT_KEY, Toast.LENGTH_SHORT); constants.put(DURATION_LONG_KEY, Toast.LENGTH_LONG); return constants; } @ReactMethod public void show(String message, int duration) { Toast.makeText(getReactApplicationContext(), message, duration).show(); } }
  18. public class ReactImageManager extends SimpleViewManager<ReactImageView> { @Override public String getName()

    { return "RCTImageView"; } @Override public ReactImageView createViewInstance(ThemedReactContext context) { return new ReactImageView(context, Fresco.newDraweeControllerBuilder(), mCallerContext); } }
  19. public class ReactImageManager extends SimpleViewManager<ReactImageView> { @Override public String getName()

    { return "RCTImageView"; } @Override public ReactImageView createViewInstance(ThemedReactContext context) { return new ReactImageView(context, Fresco.newDraweeControllerBuilder(), mCallerContext); } @ReactProp(name = "src") public void setSrc(ReactImageView view, @Nullable String src) { view.setSource(src); } @ReactProp(name = "borderRadius", defaultFloat = 0f) public void setBorderRadius(ReactImageView view, float borderRadius) { view.setBorderRadius(borderRadius); } @ReactProp(name = ViewProps.RESIZE_MODE) public void setResizeMode(ReactImageView view, @Nullable String resizeMode) { view.setScaleType(ImageResizeMode.toScaleType(resizeMode)); } }
  20. React Native • Try it out • Build an app

    • Submit issues and pull requests