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
- 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?
- "learn once, write everywhere"
- JS (ships with mix of es6 and es7 Babel transforms)
- turly 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
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
- 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 Queuerunning 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
- app I wrote before this talk
- boiler plate app w/ redux
- react native playground
- these slides
React Native Android
An Intro!
Software Engineering Club
Rm 6-12. Jan 20. 11am
Christine Yu / http://github.com/cy