React Native Android – An Intro!



React Native Android – An Intro!

0 1


react-native-talk

http://christine-yu.com/react-native-talk

On Github cy / react-native-talk

React Native Android

An Intro!

Software Engineering Club

Rm 6-12. Jan 20. 11am

Christine Yu / http://github.com/cy

What

  • framework to build Native mobile apps using JS and React
  • used by fb for Groups and Ads manager apps
  • iOS: Jan 2015, Android: Sept 2015

Why Not __

  • Native SDKs
    • learning curve: seperate paradigms
    • maintaince: seperate codebases
    • compiling takes a long time
  • Cordova/PhoneGap
    • wrapped webview feels toyish
    • slow performance
  • NativeScript
    • no react! imperative
  • Crosscompiled Xamarin (C#), Marmalade (C++)
    • can't use native SDK elements
    • doesn't look native on any platform
    • lots of control, very manual
- using a compiler to compile code down to binaries that will run on a different platform. - Marmalade use GCC ARM to compile for Android, GCC x86 to compile for simulator (has own simulator)

Why React Native?

React Basics

  • Declarative approach
  • everything is a component (vs controllers, directives, services in Angular 1)
  • component communication via props
  • solves the problem of stateful ui by rerendering UI whenever the state or props changes
    • calculates a diff
render() {
  return(
    <View>
      <Text>Hello World!</Text>
    </View>
  );
}

React Native Basics

  • JSX wraps native elemts:
    • View, ListView
    • Text
    • TouchableOpacity, TouchableHighLight
    • Navigator
  • Subset of CSS properties to style
    • Flexbox for layout

How does Native work?

Under the Hood of React Native talk, https://www.youtube.com/watch?v=8N4f4h6SThc)

  • JS running in JavaScriptCore JSVM controlling Native through a C++/Java bridge
  • platform specific code are 'infixed' by platform name
  • packager starts a local webserver and sends a JS bundle to device
      <Text /> => new TextView(getContext());
    
  • Can define your own & publish: https://react.parts/native
- gradle to assemble release apk - Native asks to start the app, and starts JSVM - JSVM runs developer's JS code, ie. give me some views - goes back to Native, Native goes here are some views - solves potential bottlenecks by running three threads, UI Event Queue, Native Modules Event Queue and JS Event Queue

dev Setup

running for dev

$ react-native init # generates hello world proj
$ react-native start # starts local webserver
$ adb reverse tcp:8081 tcp:8081 # enables device GET of js bundle from local webserver
$ react-native run-android # starts the app on device

packaging apk

$./gradlew assembleRelease

tools

  • android monitor
  • android avd emulator (slow even with accelerator, test on device)
  • live reloading or shake to reload
  • chrome debugger
  • element inspector

cons

  • Navigator awkward to use
    • pass props in the route?
    • switch statement in render?
  • Reload always restarts at the first scene
    • trick: set the intial scene to the one you're working on
  • Still new
    • few modules (no offical camera, accelerometer apis)
- unexpected, not to spec errors:
<View style={{borderWidth: 1, ...style}}>
<View style={[{paddingLeft:1}, ...style]}>

extras

React Native Android An Intro! Software Engineering Club Rm 6-12. Jan 20. 11am Christine Yu / http://github.com/cy