20180518 QNAP Seminar - Introduction to React Native
Nov. 12, 2020•0 likes
1 likes
Be the first to like this
Show More
•213 views
views
Total views
0
On Slideshare
0
From embeds
0
Number of embeds
0
Download to read offline
Report
Software
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
20180518 QNAP Seminar - Introduction to React Native
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
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 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');
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
● 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
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.
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.
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
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
● ...
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
Editor's Notes
state may be updated asynchronously and we should not rely on its values for calculating the next state