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

React Native: Are we there yet? (Pokémon edition) @ VinnytsiaJS '16

React Native: Are we there yet? (Pokémon edition) @ VinnytsiaJS '16

Roman Liutikov

July 31, 2016
Tweet

More Decks by Roman Liutikov

Other Decks in Programming

Transcript

  1. Native mobile development with JavaScript PhoneGap — WebView Tabris.js —

    native UI NativeScript — native UI, Angular 2, TypeScript React Native — native UI, React.js
  2. React Native • Released in 2015 • Open source •

    Maintained by Facebook and community • Cross-platform mobile development (iOS, Android, UWP) • Based on React • New release every 2 weeks
  3. Benefits for developers • Use language that you already know

    — JavaScript • Use UI library that you already know — React • Develop cross-platform applications • Interactive development, because JavaScript is dynamic language • Send instant updates to your users, no need to submit to App Store
  4. How it works • Bridge between native code and JavaScript

    • 2 main threads ◦ Main thread — create and position views ◦ JavaScript thread — your code • Dispatch queues (more efficient at executing tasks) ◦ Shadow queue — layout calculation ◦ + one queue for every native module • React Native Architecture Overview
  5. Getting started • React Native CLI npm i -g react-native-cli

    • MacBook, Xcode, iOS device/simulator, Apple Developer Program ($100) • Java, Android SDK, Android device/simulator • Chrome’s DevTools • Integrated development tools • React Native CodePush
  6. There’s no... • No HTML • No CSS • No

    DOM APIs • No Web APIs There are no web technologies, it’s just JavaScript.
  7. Views: JSX → VDOM → Native views <View> <Text style={styles.text}>Hello!</Text>

    <TextInput value={val} onChangeText={handleChange} /> <TouchableHighlight onPress={handlePress}> <Text>Submit</Text> </TouchableHighlight> </View>
  8. Styles: Subset of CSS and Flexbox var styles = StyleSheet.create({

    container: { flex: 1, alignItems: 'center' } }); var styles = StyleSheet.create({ button: { backgroundColor: 'blue', borderColor: 'red', borderWidth: StyleSheet.hairlineWidth } });
  9. Styling gotchas • Use StyleSheet.create to create performant style definitions

    • Cross-platform zIndex, kind of • There’s no cross-platform shadows, only for iOS • Dimensions are set in points, use StyleSheet.hairlineWidth to get size of 1px in points
  10. UI Events • No generic click / onchange events •

    Use TouchableHighlight for buttons and “links” • Use onStartShouldSetResponder to capture touch events on View
  11. 100% shared code is not possible • Platform-specific components •

    Different Design Guidelines • Different behaviour • Different interactions iOS Human Interface Guidelines Android UI Guide
  12. What can be shared? • Content UI (forms, news feed,

    ...) • Domain logic (data flow) • Data fetching
  13. How to develop cross-platform app? • Separate domain logic and

    data fetching from UI • Use Flux / Redux / Relay • Use cross-platform native modules • Use ios and android suffixes in filenames (button.ios.js, button.android.js)
  14. Native Modules • TabBarIOS, ToolbarAndroid, ViewPagerAndroid, … • Do not

    use pure JavaScript module if there’s a native one • Native modules works faster and they can do more • Use RNPM to install native modules • If there’s something missing you can always expose it to JavaScript
  15. Use in JavaScript import { NativeModules } from 'react-native'; var

    CalendarManager = NativeModules.CalendarManager; CalendarManager.addEvent( 'Birthday Party', '4 Privet Drive, Surrey');
  16. Graphics • Do not use scaled down high-res images at

    runtime, it slows down scrolling perf • Use SVG is possible (ReactNativeART)
  17. Slow initial rendering in ListView • Specify reasonable initialListSize •

    Same for pageSize <ListView initialListSize={15} pageSize={15} dataSource={dataSource} renderRow={renderRow} />
  18. Frequent re-rendering Use setNativeProps to set data directly on native

    component this.refs.input.setNativeProps({ text });
  19. Avoid expensive work during animations Use InteractionManager to schedule expensive

    work after animations / interactions InteractionManager.runAfterInteractions(() => { // long-running synchronous task... });
  20. Animations Use LayoutAnimation for simple and uncontrolled animations. Layout animation

    happens on the main thread at next layout recalculation. componentWillUpdate() { LayoutAnimation.configureNext(config); }
  21. Animated • Use it for complex and controlled animations •

    Controlled from JS thread • Imperative API
  22. Navigator <Navigator initialRoute={{ name: 'home', component: Home }} renderScene={(route, navigator)

    => { const SceneComponent = route.component; return ( <SceneComponent goTo={(name) => navigator.push({ ...routes[name] })} /> ) }} />
  23. Navigator • No nested routes • Navigator maintains a stack

    of routes • Runs transition animation between screens • Imperative API • Use NavigationExperimental • When starting a new project think about how you would implement navigation
  24. Development • Remember that application runs slower in Development mode

    • Don’t forget to enable hot-reloading • Debug in Chrome’s DevTools • Use integrated Inspector to find components, inspect styles and layout
  25. Tests • Separate domain logic domain from UI for better

    testability • Enzyme and Mocha for unit tests • Integration tests are more complex (appium.io)
  26. Current status of React Native • In active development •

    Documentation is pretty good • Quality of modules produced by community is questionable • Lots of issues • But already used to build many great apps
  27. Future of development with React Native • Better development environment

    (Deco IDE) • Isolated UI development (react-storybook) • Better performance • More platforms support • We should learn from native mobile development
  28. Reading list • Bridging in React Native • Dive into

    React Native performance • React Native Integration Tests • Performance Limitations of React Native and How to Overcome Them • Using React Native: One Year Later • Routing and Navigation in React Native • js.coach/react-native • facebook.github.io/react-native • jondot/awesome-react-native • react-native-code-push <Questions? /> @roman01la