react native
play

React Native Shan-Hung Wu & DataLab CS, NTHU Outline Hello - PowerPoint PPT Presentation

React Native Shan-Hung Wu & DataLab CS, NTHU Outline Hello React Native How it works? Components, props, and states Styling Event handling Images and icons Data access WeatherMoodMobile NativeBase


  1. React Native Shan-Hung Wu & DataLab CS, NTHU

  2. Outline • Hello React Native – How it works? – Components, props, and states – Styling – Event handling – Images and icons – Data access • WeatherMoodMobile – NativeBase – ScrollView and ListView – Navigation • Animations 2

  3. Prerequisite: HTML5 CSS3 ES6 React JS Redux 3

  4. Outline • Hello React Native – How it works? – Components, props, and states – Styling – Event handling – Images and icons – Data access • WeatherMoodMobile – NativeBase – ScrollView and ListView – Navigation • Animations 4

  5. React Native • A framework that let you write apps in React JS way 5

  6. Installation Guide > react-native init HelloReactNative // iOS > react-native run-ios // on Android, start AVD first > react-native run-android 6

  7. HelloReactNative > react-native init HelloReactNative // in HelloReactNative/index.[ios|android].js import React from 'react'; import {AppRegistry, Text} from 'react-native'; class MyApp extends React.Component { render() { return ( • Camel-case convention <Text>Hello world!</Text> • ES6 features ); } • JSX with RN components } – *.js files AppRegistry.registerComponent( • AppRegistry instead of 'HelloReactNative', () => MyApp ReactDOM ); 7

  8. Running and Dynamic Reloading • Packager is like Webpack • Reload: – Cmd + R (iOS) – R + R (Android) • Dev menu: – Cmd + D (iOS) – Cmd + M (Android) • Debugging: – console.log() – debugger 8

  9. Why app written by JS is native ? 9

  10. Outline • Hello React Native – How it works? – Components, props, and states – Styling – Event handling – Images and icons – Data access • WeatherMoodMobile – NativeBase – ScrollView and ListView – Navigation • Animations 10

  11. Native Apps Your App (C or Swift) Your App (Java or Kotlin) 3 rd Party Libs 3 rd Party Libs SDKs, Standard Libs SDKs, Standard Libs iOS Android • Different code and language for different OS's 11

  12. WebView Your App Apps Mobile Development Framework (e.g., Apache Cordova, Adobe PhoneGap) WebView API iOS SDKs Android SDKs iOS Android • Write once, run everywhere • Slow and not feeling native 12

  13. React-Native Apps Your App Your App (JS) Your App Your App (JS) (Native UI & (Native UI & Modules) Modules) NPM Pkgs NPM Pkgs (e.g., React) (e.g., React) Bridge Bridge 3 rd Party Libs 3 rd Party Libs Native UI React Native Native UI React Native iOS SDKs JS Runtime Android SDKs JS Runtime iOS Android • JS components render as native ones • Learn once, write everywhere 13

  14. Native JS Bridge (Java, C, etc.) AppRegistry AppRegistry [funID, args] .runApp('MyApp'); .runApp('MyApp'); v = UIModule return ( .createView(...); <View>...</View> [funID, args] v.render(); ); • Calls through bridge are – Asynchronous (event loops are separated) – Batched (to save overhead) 14

  15. Outline • Hello React Native – How it works? – Components, props, and states – Styling – Event handling – Images and icons – Data access • WeatherMoodMobile – NativeBase – ScrollView and ListView – Navigation • Animations 15

  16. RN Components (see Doc) • <View> is like <div> in HTML • <Text> is like <span> – Text must be wrapped in <Text>...</Text> • Custom components: // in MyComponent.js export defaut class MyComponent extends React.Component { render() { ... } } // in App.js import MyComponent from './MyComponent'; // use <MyComponent /> in render() 16

  17. Props and States, // in App.js <MyComponent name={'Bob'} /> as Usual // in MyComponent.js class MyComponent extends React.Component { constructor(props) { ... this.state = { isNew: true } } render() { const {name} = this.props; return ( <Text>Hello {name}, { this.state.isNew ? 'welcome' : 'welcome back' }</Text> ); } } 17

  18. Redux, as Usual import {connect} from 'react-redux'; class MyComponent extends React.Component { render() { const {name, isNew} = this.props; return ( <Text>Hello {name}, { isNew ? 'welcome' : 'welcome back' }</Text> ); } } export default connect(state => ({ isNew: state.user.isNew // 'user' reducer }))(MyComponent); 18

  19. Prop, State, or Redux Store? 19

  20. Outline • Hello React Native – How it works? – Components, props, and states – Styling – Event handling – Images and icons – Data access • WeatherMoodMobile – NativeBase – ScrollView and ListView – Navigation • Animations 20

  21. Styling in RN • No CSS • Instead, assign style prop to components render() { return ( <View> <Text style={{color: 'blue'}} >...</Text> <Text style={styles.red} >...</Text> // cascade <Text style={[styles.red, styles.title]} >...</Text> </View> ); • List of supported styles } const styles = { • Values have no unit red: {color: 'red'}, title: {fontSize: 24} }; 21

  22. import { StyleSheet } from StyleSheet 'react-native'; render() { return ( <View> <View style={styles.listItem}>...</View> <View style={styles.listItem}>...</View> ... <View style={styles.listItem}>...</View> </View> ); } const styles = StyleSheet.create( { listItem: {...} } ) ; • Allows multiple native components to refer to same style object (by ID) – Useful for, e.g., list items 22

  23. Sizing and Layout • Every “container” component <View> (e.g., View) is a flexbox flex: 1 – flexDirection : ‘column’ by default – justifyContent : ‘flex - start’ flex: 2 – alignItems : ‘stretch’ • Contained component: – alignSelf flex: 3 – width/height: number – flex : number • Use inspector at runtime </View> 23

  24. Outline • Hello React Native – How it works? – Components, props, and states – Styling – Event handling – Images and icons – Data access • WeatherMoodMobile – NativeBase – ScrollView and ListView – Navigation • Animations 25

  25. Event Handling render() { return ( <TouchableHighlight onPress={this.handlePress} onLongPress={() => alert('Yo')}> <View> <Text>Press me!</Text> </View> </TouchableHighlight> ); } • TouchableHighlight • TouchableOpacity • TouachableNativeFeedback (Android only) 26

  26. Controlled Components render() { return ( <TextInput placeHolder='Type here' value={this.state.text} // controlled component onChangeText={text => this.setState({text})} ref={el => this.inputEl} onEndEditing={() => { ... this.inputEl.clear(); }} /> ); } 27

  27. How are native events handled in JS? 28

  28. Native Native JS Thread UI (Main) Thread Modules Thread Event Queue Event Queue Event Queue Threads and Queues 29

  29. Native Native JS Thread UI (Main) Thread Modules Thread Event Queue Event Queue Event Queue Event • E.g., touch, I/O, or networking event 30

  30. Native Native JS Thread UI (Main) Thread Modules Thread Event Queue Event Queue Event Queue Event Run Handler • JS thread calls your handler via the bridge 31

  31. Native Native JS Thread UI (Main) Thread Modules Thread Event Queue Event Queue Event Queue Event Run Handler Layout • If UI changed in JS, module thread performs layout first (e.g., measuring size of text) 32

  32. Native Native JS Thread UI (Main) Thread Modules Thread Event Queue Event Queue Event Queue Event Run Handler Update UI Layout • Then, UI thread renders components 33

  33. Native Native JS Thread UI (Main) Thread Modules Thread Ideally, entire cycle in 16ms (60fps) • Offload unnecessary computing to bg – Using, e.g., Promise API Event Queue Event Queue Event Queue Touch Event Run Handler Update UI Layout 34

  34. Outline • Hello React Native – How it works? – Components, props, and states – Styling – Event handling – Images and icons – Data access • WeatherMoodMobile – NativeBase – ScrollView and ListView – Navigation • Animations 35

  35. Images // JSX <Image source={require('dir/image.png')} style={{...}} /> // in dir image@2x.png // iPhone 7 image@3x.png // iPhone 7 Plus or Nexus 5 • RN handles off-thread decoding for you • Size inferred from source file by default – To scale image dynamically (with flex ), set width and height to undefined • Background image? <Image source={...} resizeMode='cover' style={{...}}> <View>...</View> </Image> 36

  36. Network Images <Image source={{ uri: 'https://.../image.png', cache: 'reload' // or 'force-cache' or 'only-if-cached' }} style={{width: 200, height: 200}} onLoad={...} /> • RN handles caching for you • But you need to specify size manually • It's a good practice to display a static placeholder before loaded // in JSX {this.state.isLoaded ? <Image source={{uri: ...}} onLoad={() => this.setState({isLoaded: true})} /> : <Image source={require('dir/placeholder.png')}>} 37

  37. Font Icons > npm install --save react-native-vector-icons > react-native link // in JS import Icon from 'react-native-vector-icons/FontAwesome'; // JSX <Icon name="rocket" size={30} color="#900" /> • See more supported fonts and features 38

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend