SlideShare a Scribd company logo
From Web to Native Mobile App
Eric Deng
2018/05/18 QNAP Seminar
Outline
● What is React Native?
● How does React Native work?
● Writing React Native
○ Expo
○ Components, props, and states
○ Component lifecycle
○ Declarative and imperative
○ Event handling
○ User input
○ Style
○ Layout
○ Data access
● Publishing your Project
What is React Native?
● A framework that let you build native mobile apps using JavaScript
● Relies on React core - A JavaScript library for building user interfaces
○ Component-based
○ High reusability
○ Learn once, write everywhere
● Supports iOS and Android
Why app written by JS is native?
Native App
● Different code and language for different OS
WebView App
● Write once, run everywhere
● Slow and not feeling native
React Native App
● JS components render as native ones
● Difference: JS Runtime, components for specific OS
How does React Native work?
Native
UI (Main) Thread
Native
Modules Thread Bridge JS Thread
Event Queue Event Queue Event Queue
Separate threads for UI, layout and JavaScript
[funID, args]
[funID, args]
Native
UI (Main) Thread
Native
Modules Thread Bridge JS Thread
Event Queue Event Queue Event Queue
E.g., touch, I/O, or networking event
Event
[funID, args]
[funID, args]
Native
UI (Main) Thread
Native
Modules Thread Bridge JS Thread
Event Queue Event Queue Event Queue
Handler
JS thread calls your handler via the bridge
● Asynchronous (event loop are separated)
● Batched (to save overhead)
[funID, args]
[funID, args]
Event
Native
UI (Main) Thread
Native
Modules Thread Bridge JS Thread
Event Queue Event Queue Event Queue
Event Handler
[funID, args]
[funID, args]
Layout
If UI changed in JS, module thread performs
layout first (e.g., measuring size of text)
Update UI
UI thread renders
components
Writing React Native
Expo
Suite of tools to accelerate the React Native development process
● XDE - a GUI to serve, share, and publish your projects
● CLI - a command-line interface to serve, share, and publish your projects
● Client - runs your projects on your phone while developing
● SDK - bundles and exposes cross-platform libraries and APIs
● Snack - React Native online code editor
React Native Packager Device logs
== Webpack
20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React Native
App.js -
root component
ES6+ features
React Native Components
● import {View, Text, Button} from ‘react-native’
● <View> is like <div> in HTML
● <Text> → <span>
○ Text must be wrapped in <Text>...</Text>
● <Button> → <button>
https://facebook.github.io/react-native/docs/components-and-apis.html
● Custom components
JSX: XML-like syntax extension of JavaScript
Compiled to normal objects by Babel:
React.createElement(Text, {}, 'Hello React Native');
React.Component
render() accepts only a single root element
Two types of data that control a component
● props
● state
props
props
● Changes in props will cause re-renders
● Can be any JS value
● Never change props in a component!
○ So, React can efficiently detect whether a component should be re-
rendered
state
internally-managed configuration for a component
state
● Can only be updated by invoking `this.setState()`
○ Implemented in React.Component
○ `setState()` calls are batched and run asynchronously
○ Changes in state also cause re-renders
state
Error!
ES7 property initializer
this.handleName = this.handleName.bind(this);
state
● If new state depends on previous one (or props)
Less reliable
Lifting State Up
Component Lifecycle
https://github.com/wojtekmaj/react-lifecycle-methods-diagram
avoid!
Component Lifecycle - Mounting
1. constructor() → Ext.Component.initComponent()
○ Initialize state or other class properties
2. render() → Ext.Component.render()
○ Return a node
○ Necessary!
3. componentDidMount() → Ext.Component.afterrender()
○ Do anything that isn’t needed for UI (async actions, network requests, timers, etc.)
Component Lifecycle - Updating
1. shouldComponentUpdate(nextProps, nextState) →
Ext.Component.beforerender()
○ Compare changed values, return true if the component should rerender
○ If returned false, the update cycle terminates
2. render() → Ext.Component.render()
3. componentDidUpdate(prevProps, prevState) → Ext.Component.afterrender()
○ Do anything that isn’t needed for UI.
Component Lifecycle - Unmounting
● componentWillUnmount() → Ext.Component.destroy()
○ Clean up
■ Remove event listeners
■ Clear timeouts/intervals
■ Invalidate network requests
● Fetch API: AbortController.abort()
● socket.close()
1
2
3
4
6
7
5
React is declarative
● Imperative vs Declarative Programming
● Debug-friendly
React is declarative
● Imperative vs Declarative Programming
● Debug-friendly
When writing React,
not to think of how you want to accomplish a result,
but instead what the component should look like in its new state.
Event Handling
● <Button title=”” onPress={handle function} />
● <TouchableHighlight>
● <TouchableOpacity>
● <TouchableWithoutFeedback>
● <TouachableNativeFeedback> (Android only)
User input
Controlled component
Uncontrolled component
Style
● Native design
● ex: <Button> import from react-native
iOS Android
Style
values have no unit
pass an array of styles
css properties are written using camel casing
assign style prop to components
Style
● StyleSheet.create()
○ Additional optimization: Allows multiple native components to refer to same style object (by ID)
Layout
● Container component is default to Flexbox layout
○ flexDirection: ‘column’
○ justifyContent: ‘flex-start’ (alignment along the main axis)
○ alignItems: ‘stretch’ (alignment along the cross axis)
● Contained component:
○ width/height: number
○ alignSelf: auto|stretch|center|flex-start|flex-end|baseline|initial
○ flex: number
main axis
cross axis
flex: 1
flex: 2
flex: 3
Data access
Fetch API
HTML5 Web API
Apps on Apple's App Store shall use HTTPS
Publishing your Project
1. Set metadata in app.json
○ https://docs.expo.io/versions/latest/workflow/configuration
2. Build the app using exp (Expo CLI)
○ Install with `npm install --global exp`
○ Build .apk with `exp build:ios` for Android
○ Build .ipa with `exp build:android` for iOS
○ Run `exp build:status` and paste the url in a browser to download
3. Upload to the appropriate store
○ https://docs.expo.io/versions/latest/distribution/building-standalone-apps
○ https://docs.expo.io/versions/latest/distribution/app-stores
Summary
1. Component-based
2. JS components render as native ones
3. Separate threads for UI, layout and JavaScript
4. Props are fixed throughout the lifetime of a component
5. setState() calls are batched and run asynchronously
6. Component lifecycle: Mounting, Updating, Unmounting
7. Declarative programming
8. Controlled component
More
● ScrollView
● Lists
○ FlatList
○ SectionList
● Persistent Storage - AsyncStorage
● React-navigation - routing and navigation for your React Native apps
● Redux - predictable state container for JavaScript apps
● ...
Reference
React Native
React
Expo
Mobile App Development with React Native
Q&A
Thank you!
20180518 QNAP Seminar - Introduction to React Native
Differences between React Native and React (Web)
● Base components
● Event Handling
● Style
● No browser APIs
○ Some have been polyfilled (fetch, timers, console, etc.)
● Navigation
Fast Re-rendering
● React keeps a virtual DOM in memory
○ Diffs the virtual DOM against the real DOM
○ Updates only changed elements in real ROM
● Reconciliation

More Related Content

What's hot (20)

PPTX
A Brief Introduction to React.js
Doug Neiner
 
PPTX
React JS - A quick introduction tutorial
Mohammed Fazuluddin
 
PDF
Understanding react hooks
Samundra khatri
 
PDF
Introduction to React JS
Bethmi Gunasekara
 
PDF
React JS - Introduction
Sergey Romaneko
 
PPT
React native
Mohammed El Rafie Tarabay
 
PDF
Introduction to react native
Dani Akash
 
PPTX
Reactjs
Neha Sharma
 
PPTX
Introduction to React
Rob Quick
 
PDF
Workshop 21: React Router
Visual Engineering
 
PPTX
Introduction to react_js
MicroPyramid .
 
PDF
React Native in a nutshell
Brainhub
 
PDF
Intro To React Native
FITC
 
PPTX
React workshop presentation
Bojan Golubović
 
PDF
Introduction to Redux
Ignacio Martín
 
PPTX
React js
Oswald Campesato
 
PDF
React Context API
NodeXperts
 
PPTX
React JS: A Secret Preview
valuebound
 
PPTX
Intro to React
Justin Reock
 
PDF
Introduction to React Native
Sambhu Lakshmanan
 
A Brief Introduction to React.js
Doug Neiner
 
React JS - A quick introduction tutorial
Mohammed Fazuluddin
 
Understanding react hooks
Samundra khatri
 
Introduction to React JS
Bethmi Gunasekara
 
React JS - Introduction
Sergey Romaneko
 
Introduction to react native
Dani Akash
 
Reactjs
Neha Sharma
 
Introduction to React
Rob Quick
 
Workshop 21: React Router
Visual Engineering
 
Introduction to react_js
MicroPyramid .
 
React Native in a nutshell
Brainhub
 
Intro To React Native
FITC
 
React workshop presentation
Bojan Golubović
 
Introduction to Redux
Ignacio Martín
 
React Context API
NodeXperts
 
React JS: A Secret Preview
valuebound
 
Intro to React
Justin Reock
 
Introduction to React Native
Sambhu Lakshmanan
 

Similar to 20180518 QNAP Seminar - Introduction to React Native (20)

PPTX
Session 03_04-Working with React Native.pptx
VHiu94
 
PDF
React native: building native iOS apps with javascript
Polidea
 
PDF
Introduzione a React Native - Facebook Developer Circle Rome
Matteo Manchi
 
PPTX
Intro to React Native
ForSharing
 
PPTX
Introduction to React Native
Waqqas Jabbar
 
PDF
React native
Vishal Dubey
 
PPTX
React native
Vikrant Negi
 
PDF
"React Native" by Vanessa Leo e Roberto Brogi
ThinkOpen
 
PDF
Matteo Manchi - React Native for multi-platform mobile applications - Codemot...
Codemotion
 
PPTX
React native
Jacob Nelson
 
PPTX
Introduction to react native @ TIC NUST
Waqqas Jabbar
 
PPTX
Introduction to React Native
Waqqas Jabbar
 
PPTX
Creating books app with react native
Ali Sa'o
 
PPTX
React native introduction (Mobile Warsaw)
Jarek Potiuk
 
PPTX
Introduction to React native
Dhaval Barot
 
PPTX
Lecture 1 Introduction to React Native.pptx
GevitaChinnaiah
 
PPTX
Session 01_02-Introduction to React Native .pptx
VHiu94
 
PDF
l1-reactnativeintroduction-160816150540.pdf
Hương Trà Pé Xjnk
 
PPTX
Hands on react native
Jay Nagar
 
PPTX
JS Fest 2018. Илья Иванов. Введение в React-Native
JSFestUA
 
Session 03_04-Working with React Native.pptx
VHiu94
 
React native: building native iOS apps with javascript
Polidea
 
Introduzione a React Native - Facebook Developer Circle Rome
Matteo Manchi
 
Intro to React Native
ForSharing
 
Introduction to React Native
Waqqas Jabbar
 
React native
Vishal Dubey
 
React native
Vikrant Negi
 
"React Native" by Vanessa Leo e Roberto Brogi
ThinkOpen
 
Matteo Manchi - React Native for multi-platform mobile applications - Codemot...
Codemotion
 
React native
Jacob Nelson
 
Introduction to react native @ TIC NUST
Waqqas Jabbar
 
Introduction to React Native
Waqqas Jabbar
 
Creating books app with react native
Ali Sa'o
 
React native introduction (Mobile Warsaw)
Jarek Potiuk
 
Introduction to React native
Dhaval Barot
 
Lecture 1 Introduction to React Native.pptx
GevitaChinnaiah
 
Session 01_02-Introduction to React Native .pptx
VHiu94
 
l1-reactnativeintroduction-160816150540.pdf
Hương Trà Pé Xjnk
 
Hands on react native
Jay Nagar
 
JS Fest 2018. Илья Иванов. Введение в React-Native
JSFestUA
 
Ad

Recently uploaded (20)

PPTX
IObit Driver Booster Pro Crack Download Latest Version
chaudhryakashoo065
 
PPTX
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
 
PPTX
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
 
PDF
>Wondershare Filmora Crack Free Download 2025
utfefguu
 
PPTX
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
 
PPTX
B2C EXTRANET | EXTRANET WEBSITE | EXTRANET INTEGRATION
philipnathen82
 
PPTX
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
 
PDF
Why Edge Computing Matters in Mobile Application Tech.pdf
IMG Global Infotech
 
PDF
Automated Testing and Safety Analysis of Deep Neural Networks
Lionel Briand
 
PPTX
For my supp to finally picking supp that work
necas19388
 
PPTX
EO4EU Ocean Monitoring: Maritime Weather Routing Optimsation Use Case
EO4EU
 
PPTX
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
 
PDF
How DeepSeek Beats ChatGPT: Cost Comparison and Key Differences
sumitpurohit810
 
PDF
Automated Test Case Repair Using Language Models
Lionel Briand
 
PDF
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
 
PDF
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
 
PPTX
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
 
PDF
Cloud computing Lec 02 - virtualization.pdf
asokawennawatte
 
PDF
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
 
PDF
What Is an Internal Quality Audit and Why It Matters for Your QMS
BizPortals365
 
IObit Driver Booster Pro Crack Download Latest Version
chaudhryakashoo065
 
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
 
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
 
>Wondershare Filmora Crack Free Download 2025
utfefguu
 
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
 
B2C EXTRANET | EXTRANET WEBSITE | EXTRANET INTEGRATION
philipnathen82
 
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
 
Why Edge Computing Matters in Mobile Application Tech.pdf
IMG Global Infotech
 
Automated Testing and Safety Analysis of Deep Neural Networks
Lionel Briand
 
For my supp to finally picking supp that work
necas19388
 
EO4EU Ocean Monitoring: Maritime Weather Routing Optimsation Use Case
EO4EU
 
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
 
How DeepSeek Beats ChatGPT: Cost Comparison and Key Differences
sumitpurohit810
 
Automated Test Case Repair Using Language Models
Lionel Briand
 
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
 
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
 
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
 
Cloud computing Lec 02 - virtualization.pdf
asokawennawatte
 
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
 
What Is an Internal Quality Audit and Why It Matters for Your QMS
BizPortals365
 
Ad

20180518 QNAP Seminar - Introduction to React Native

  • 1. From Web to Native Mobile App Eric Deng 2018/05/18 QNAP Seminar
  • 2. Outline ● What is React Native? ● How does React Native work? ● Writing React Native ○ Expo ○ Components, props, and states ○ Component lifecycle ○ Declarative and imperative ○ Event handling ○ User input ○ Style ○ Layout ○ Data access ● Publishing your Project
  • 3. What is React Native? ● A framework that let you build native mobile apps using JavaScript ● Relies on React core - A JavaScript library for building user interfaces ○ Component-based ○ High reusability ○ Learn once, write everywhere ● Supports iOS and Android
  • 4. Why app written by JS is native?
  • 5. Native App ● Different code and language for different OS
  • 6. WebView App ● Write once, run everywhere ● Slow and not feeling native
  • 7. React Native App ● JS components render as native ones ● Difference: JS Runtime, components for specific OS
  • 8. How does React Native work?
  • 9. Native UI (Main) Thread Native Modules Thread Bridge JS Thread Event Queue Event Queue Event Queue Separate threads for UI, layout and JavaScript [funID, args] [funID, args]
  • 10. Native UI (Main) Thread Native Modules Thread Bridge JS Thread Event Queue Event Queue Event Queue E.g., touch, I/O, or networking event Event [funID, args] [funID, args]
  • 11. Native UI (Main) Thread Native Modules Thread Bridge JS Thread Event Queue Event Queue Event Queue Handler JS thread calls your handler via the bridge ● Asynchronous (event loop are separated) ● Batched (to save overhead) [funID, args] [funID, args] Event
  • 12. Native UI (Main) Thread Native Modules Thread Bridge JS Thread Event Queue Event Queue Event Queue Event Handler [funID, args] [funID, args] Layout If UI changed in JS, module thread performs layout first (e.g., measuring size of text) Update UI UI thread renders components
  • 14. Expo Suite of tools to accelerate the React Native development process ● XDE - a GUI to serve, share, and publish your projects ● CLI - a command-line interface to serve, share, and publish your projects ● Client - runs your projects on your phone while developing ● SDK - bundles and exposes cross-platform libraries and APIs ● Snack - React Native online code editor
  • 15. React Native Packager Device logs == Webpack
  • 20. React Native Components ● import {View, Text, Button} from ‘react-native’ ● <View> is like <div> in HTML ● <Text> → <span> ○ Text must be wrapped in <Text>...</Text> ● <Button> → <button> https://facebook.github.io/react-native/docs/components-and-apis.html ● Custom components JSX: XML-like syntax extension of JavaScript Compiled to normal objects by Babel: React.createElement(Text, {}, 'Hello React Native');
  • 22. render() accepts only a single root element
  • 23. Two types of data that control a component ● props ● state
  • 24. props
  • 25. props ● Changes in props will cause re-renders ● Can be any JS value ● Never change props in a component! ○ So, React can efficiently detect whether a component should be re- rendered
  • 27. state ● Can only be updated by invoking `this.setState()` ○ Implemented in React.Component ○ `setState()` calls are batched and run asynchronously ○ Changes in state also cause re-renders
  • 29. state ● If new state depends on previous one (or props) Less reliable
  • 32. Component Lifecycle - Mounting 1. constructor() → Ext.Component.initComponent() ○ Initialize state or other class properties 2. render() → Ext.Component.render() ○ Return a node ○ Necessary! 3. componentDidMount() → Ext.Component.afterrender() ○ Do anything that isn’t needed for UI (async actions, network requests, timers, etc.)
  • 33. Component Lifecycle - Updating 1. shouldComponentUpdate(nextProps, nextState) → Ext.Component.beforerender() ○ Compare changed values, return true if the component should rerender ○ If returned false, the update cycle terminates 2. render() → Ext.Component.render() 3. componentDidUpdate(prevProps, prevState) → Ext.Component.afterrender() ○ Do anything that isn’t needed for UI.
  • 34. Component Lifecycle - Unmounting ● componentWillUnmount() → Ext.Component.destroy() ○ Clean up ■ Remove event listeners ■ Clear timeouts/intervals ■ Invalidate network requests ● Fetch API: AbortController.abort() ● socket.close()
  • 36. React is declarative ● Imperative vs Declarative Programming ● Debug-friendly
  • 37. React is declarative ● Imperative vs Declarative Programming ● Debug-friendly When writing React, not to think of how you want to accomplish a result, but instead what the component should look like in its new state.
  • 38. Event Handling ● <Button title=”” onPress={handle function} /> ● <TouchableHighlight> ● <TouchableOpacity> ● <TouchableWithoutFeedback> ● <TouachableNativeFeedback> (Android only)
  • 40. Style ● Native design ● ex: <Button> import from react-native iOS Android
  • 41. Style values have no unit pass an array of styles css properties are written using camel casing assign style prop to components
  • 42. Style ● StyleSheet.create() ○ Additional optimization: Allows multiple native components to refer to same style object (by ID)
  • 43. Layout ● Container component is default to Flexbox layout ○ flexDirection: ‘column’ ○ justifyContent: ‘flex-start’ (alignment along the main axis) ○ alignItems: ‘stretch’ (alignment along the cross axis) ● Contained component: ○ width/height: number ○ alignSelf: auto|stretch|center|flex-start|flex-end|baseline|initial ○ flex: number main axis cross axis flex: 1 flex: 2 flex: 3
  • 44. Data access Fetch API HTML5 Web API Apps on Apple's App Store shall use HTTPS
  • 45. Publishing your Project 1. Set metadata in app.json ○ https://docs.expo.io/versions/latest/workflow/configuration 2. Build the app using exp (Expo CLI) ○ Install with `npm install --global exp` ○ Build .apk with `exp build:ios` for Android ○ Build .ipa with `exp build:android` for iOS ○ Run `exp build:status` and paste the url in a browser to download 3. Upload to the appropriate store ○ https://docs.expo.io/versions/latest/distribution/building-standalone-apps ○ https://docs.expo.io/versions/latest/distribution/app-stores
  • 46. Summary 1. Component-based 2. JS components render as native ones 3. Separate threads for UI, layout and JavaScript 4. Props are fixed throughout the lifetime of a component 5. setState() calls are batched and run asynchronously 6. Component lifecycle: Mounting, Updating, Unmounting 7. Declarative programming 8. Controlled component
  • 47. More ● ScrollView ● Lists ○ FlatList ○ SectionList ● Persistent Storage - AsyncStorage ● React-navigation - routing and navigation for your React Native apps ● Redux - predictable state container for JavaScript apps ● ...
  • 48. Reference React Native React Expo Mobile App Development with React Native
  • 49. Q&A
  • 52. Differences between React Native and React (Web) ● Base components ● Event Handling ● Style ● No browser APIs ○ Some have been polyfilled (fetch, timers, console, etc.) ● Navigation
  • 53. Fast Re-rendering ● React keeps a virtual DOM in memory ○ Diffs the virtual DOM against the real DOM ○ Updates only changed elements in real ROM ● Reconciliation

Editor's Notes

  • #30: state may be updated asynchronously and we should not rely on its values for calculating the next state
  • #36: Component Lifecycle simple example
  • #37: https://codeburst.io/declarative-vs-imperative-programming-a8a7c93d9ad2
  • #38: https://codeburst.io/declarative-vs-imperative-programming-a8a7c93d9ad2
  • #40: https://goshakkk.name/controlled-vs-uncontrolled-inputs-react/