自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

React Native入門項(xiàng)目與解析

移動(dòng)開發(fā) iOS
React Native是最近非常火的一個(gè)話題,介紹如何利用React Native進(jìn)行開發(fā)的文章和書籍多如牛毛,但面向入門水平并介紹它工作原理的文章卻寥寥無幾。下面給大家詳細(xì)介紹一下。

 通過React Native 環(huán)境搭建和創(chuàng)建項(xiàng)目(Mac)可以成功創(chuàng)建一個(gè)新項(xiàng)目,即直接利用以下語句創(chuàng)建:

  1. //命令行創(chuàng)建項(xiàng)目: 
  2.  
  3. react-native init AwesomeProject 

創(chuàng)建成功后,剛?cè)腴T的我們主要關(guān)注兩個(gè)文件:

1)iOS項(xiàng)目目錄下的AppDelegate.m

為將iOS項(xiàng)目連接js文件的入口,以及相關(guān)初始化操作。

2)根目錄下的index.ios.js

為iOS對(duì)應(yīng)的js入口文件。

一、 解析iOS項(xiàng)目中的AppDelegate.m

1. AppDelegate.m 代碼如下:

  1. #import "AppDelegate.h" 
  2.  
  3. // React Native相關(guān)頭文件 
  4.  
  5. #import "RCTBundleURLProvider.h" 
  6.  
  7. #import "RCTRootView.h" 
  8.  
  9. @implementation AppDelegate 
  10.  
  11. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
  12.  
  13.  
  14. NSURL *jsCodeLocation; 
  15.  
  16. /* 
  17.  
  18. 當(dāng)應(yīng)用開始運(yùn)行的時(shí)候,RCTRootView將會(huì)從以下的URL中加載應(yīng)用:(本地調(diào)試的時(shí)候是直接在本地服務(wù)器中的index.ios加載,發(fā)布時(shí)設(shè)置有所不同) 
  19.  
  20. 重新調(diào)用了你在運(yùn)行這個(gè)App時(shí)打開的終端窗口,它開啟了一個(gè) packager 和 server 來處理上面的請(qǐng)求。 
  21.  
  22. 在 Safari 中打開那個(gè) URL;你將會(huì)看到這個(gè) App 的 JavaScript 代碼 
  23.  
  24. */ 
  25.  
  26. [[RCTBundleURLProvider sharedSettings] setDefaults]; 
  27.  
  28. jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil]; 
  29.  
  30. // RCTRootView是一個(gè)UIView容器,承載著React Native應(yīng)用。同時(shí)它也提供了一個(gè)聯(lián)通原生端和被托管端的接口。 
  31.  
  32. RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation 
  33.  
  34. moduleName:@"AwesomeProject" 
  35.  
  36. initialProperties:nil 
  37.  
  38. launchOptions:launchOptions]; 
  39.  
  40. rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1]; 
  41.  
  42. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
  43.  
  44. UIViewController *rootViewController = [UIViewController new]; 
  45.  
  46. rootViewController.view = rootView; 
  47.  
  48. self.window.rootViewController = rootViewController; 
  49.  
  50. [self.window makeKeyAndVisible]; 
  51.  
  52. return YES; 
  53.  
  54.  
  55. @end 

2. RCTRootView

RCTRootView將React Natvie視圖封裝到原生組件中。(用戶能看到的一切內(nèi)容都來源于這個(gè)RootView,所有的初始化工作也都在這個(gè)方法內(nèi)完成。)

解析:

通過RCTRootView的初始化函數(shù)你可以將任意屬性傳遞給React Native應(yīng)用。

參數(shù)initialProperties必須是NSDictionary的一個(gè)實(shí)例。

這一字典參數(shù)會(huì)在內(nèi)部被轉(zhuǎn)化為一個(gè)可供JS組件調(diào)用的JSON對(duì)象。

  1. NSArray *imageList = @[@"http://foo.com/bar1.png"
  2.  
  3. @"http://foo.com/bar2.png"]; 
  4.  
  5. NSDictionary *propsDict = @{@"images" : imageList}; 
  6.  
  7. RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation 
  8.  
  9. moduleName:@"AwesomeProject" 
  10.  
  11. initialProperties: propsDict 
  12.  
  13. launchOptions:launchOptions]; 

在js文件中,則是通過this.props.images調(diào)用上面定義的參數(shù)。

this為AppRegistry.registerComponent注冊(cè)的組件(下面會(huì)講到)

RCTRootView同樣提供了一個(gè)可讀寫的屬性appProperties。在appProperties設(shè)置之后,React Native應(yīng)用將會(huì)根據(jù)新的屬性重新渲染。當(dāng)然,只有在新屬性和之前的屬性有區(qū)別時(shí)更新才會(huì)被觸發(fā)。

(注意:1.可以隨時(shí)更新屬性,但是更新必須在主線程中進(jìn)行,讀取則可以在任何線程中進(jìn)行。2.更新屬性時(shí)并不能做到只更新一部分屬性)

  1. NSArray *imageList = @[@"http://foo.com/bar3.png"
  2.  
  3. @"http://foo.com/bar4.png"]; 
  4.  
  5. rootView.appProperties = @{@"images" : imageList}; 

二、解析js入口文件(index.ios.js)

1. index.ios.js 代碼如下:

  1. 'use strict'; // 全局進(jìn)入嚴(yán)格模式(目前發(fā)現(xiàn)不用也行) 
  2.  
  3. /**< 
  4.  
  5. 消除Javascript語法的一些不合理、不嚴(yán)謹(jǐn)之處,減少一些怪異行為; 
  6.  
  7. 消除代碼運(yùn)行的一些不安全之處,保證代碼運(yùn)行的安全; 
  8.  
  9. 提高編譯器效率,增加運(yùn)行速度; 
  10.  
  11. 為未來新版本的Javascript做好鋪墊。 
  12.  
  13. */ 
  14.  
  15. //導(dǎo)入一些必要的模塊 
  16.  
  17. //React Native內(nèi)置的組件可以直接通過import { xxx } from 'react-native' 進(jìn)行導(dǎo)入,當(dāng)然也可以自定義組件。 
  18.  
  19. import React, { Component } from 'react'
  20.  
  21. import { 
  22.  
  23. AppRegistry, 
  24.  
  25. StyleSheet, 
  26.  
  27. Text, 
  28.  
  29. View
  30.  
  31. TouchableOpacity 
  32.  
  33. from 'react-native'
  34.  
  35. //類,這是默認(rèn)的載入類,繼承自Component,Component類(組件)似于Android和iOS中的View 
  36.  
  37. //這里為創(chuàng)建一個(gè)組件 
  38.  
  39. class AwesomeProject extends Component { 
  40.  
  41. //構(gòu)造器 ,每個(gè)組件都擁有自己的屬性(props)和狀態(tài)(state) 
  42.  
  43. //調(diào)用this.setState更改狀態(tài)text或者isTouchDown時(shí),組件會(huì)觸發(fā)render函數(shù)進(jìn)行渲染更新 
  44.  
  45. constructor(props) { 
  46.  
  47. super(props); 
  48.  
  49. this.state = { 
  50.  
  51. text:'Welcome to React Native!'
  52.  
  53. isTouchDown:false 
  54.  
  55. }; 
  56.  
  57.  
  58. //在最初的渲染之前調(diào)用一次,可在里面進(jìn)行預(yù)處理操作 
  59.  
  60. //在React中,設(shè)置this.state會(huì)導(dǎo)致重新渲染,但是componentWillMount設(shè)置this.state并不會(huì)對(duì)導(dǎo)致render調(diào)用多次 
  61.  
  62. //之后會(huì)對(duì)component的生命周期進(jìn)一步解釋 
  63.  
  64. componentWillMount() { 
  65.  
  66.  
  67. //渲染函數(shù),用來渲染實(shí)際的Component可視部分 
  68.  
  69. render() { 
  70.  
  71. //var定義變量,根據(jù)狀態(tài)值改變對(duì)應(yīng)值 
  72.  
  73. var welcomeText = this.state.text; 
  74.  
  75. var bgcolor; 
  76.  
  77. if (this.state.isTouchDown) { 
  78.  
  79. bgcolor = '#c5c5ab'
  80.  
  81. else { 
  82.  
  83. bgcolor = '#F5FCFF'
  84.  
  85.  
  86. console.log('testtststststts'); 
  87.  
  88. //返回的即界面顯示內(nèi)容 
  89.  
  90. return ( 
  91.  
  92. <View style={[styles.container, {backgroundColor: bgcolor}]}> 
  93.  
  94. <Text style={styles.welcome}> 
  95.  
  96. {welcomeText} 
  97.  
  98. </Text> 
  99.  
  100. <Text style={styles.instructions}> 
  101.  
  102. To get started, edit index.android.js 
  103.  
  104. </Text> 
  105.  
  106. <Text style={styles.instructions}> 
  107.  
  108. Shake or press menu button for dev menu 
  109.  
  110. </Text> 
  111.  
  112. <TouchableOpacity onPress={this.touchDown.bind(this)}> 
  113.  
  114. <Text style={[styles.instructions, {backgroundColor: 'green'}]}> 
  115.  
  116. test touch Me 
  117.  
  118. </Text> 
  119.  
  120. </TouchableOpacity> 
  121.  
  122. </View
  123.  
  124. ); 
  125.  
  126.  
  127. // 自定義函數(shù) 
  128.  
  129. touchDown() { 
  130.  
  131. // console.log 控制臺(tái)打印,可打印值,多用于調(diào)試 
  132.  
  133. console.log('>>', this.isTouchDown); 
  134.  
  135. if (!this.state.isTouchDown) { 
  136.  
  137. this.setState({ 
  138.  
  139. text:'Test Touch Down Success'
  140.  
  141. isTouchDown:true 
  142.  
  143. }); 
  144.  
  145. else { 
  146.  
  147. this.setState({ 
  148.  
  149. text:'Test Touch Down Again Success'
  150.  
  151. isTouchDown:false 
  152.  
  153. }); 
  154.  
  155.  
  156.  
  157.  
  158. //定義樣式 
  159.  
  160. const styles = StyleSheet.create({ 
  161.  
  162. container: { 
  163.  
  164. flex: 1, 
  165.  
  166. justifyContent: 'center'
  167.  
  168. alignItems: 'center'
  169.  
  170. backgroundColor: '#F5FCFF'
  171.  
  172. }, 
  173.  
  174. welcome: { 
  175.  
  176. fontSize: 20, 
  177.  
  178. textAlign: 'center'
  179.  
  180. margin: 10, 
  181.  
  182. }, 
  183.  
  184. instructions: { 
  185.  
  186. textAlign: 'center'
  187.  
  188. color: '#333333'
  189.  
  190. marginBottom: 5, 
  191.  
  192. }, 
  193.  
  194. }); 
  195.  
  196. //AppRegistry 定義了App的入口,并提供了根組件。 
  197.  
  198. //***個(gè)'AwesomeProject'要與AppDelegate里注冊(cè)的moduleName一致 
  199.  
  200. //第二個(gè)AwesomeProject則是入口組件,即上面定義的Component類 
  201.  
  202. AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject); 

2. 運(yùn)行效果:

簡(jiǎn)單運(yùn)行效果.png

3. 基礎(chǔ)概念解釋

1)組件

代碼中的 Text, View, TouchableOpacity均為基礎(chǔ)組件。AwesomeProject則是自己創(chuàng)建的組件,也作為項(xiàng)目的入口組件。

在React Native項(xiàng)目中,所有展示的界面,都可以看做是一個(gè)組件(Component)只是功能和邏輯上的復(fù)雜程度不同。每一個(gè)是許許多多小的組件拼成的,每個(gè)小的組件也有自己對(duì)應(yīng)的邏輯。

2)組件的狀態(tài)與屬性

組件本質(zhì)上是狀態(tài)機(jī),輸入確定,輸出一定確定。組件把狀態(tài)與結(jié)果一一對(duì)應(yīng)起來,組件中有state與prop(狀態(tài)與屬性)。

屬性(props)是標(biāo)簽里面的屬性, 組件之前通過標(biāo)簽的屬性來傳遞數(shù)據(jù),由父組件傳遞給子組件(單向的屬性傳遞)。

如果component的某些狀態(tài)由外部所決定,并且會(huì)影響到component的render,那么這些狀態(tài)就應(yīng)該用props表示。

例如:一個(gè)下拉菜單的component,有哪些菜單項(xiàng),是由這個(gè)component的使用者和使用場(chǎng)景決定的,那么“菜單項(xiàng)”這個(gè)狀態(tài),就應(yīng)該用props表示,并且由外部傳入。

狀態(tài)(state)是子組中的狀態(tài),內(nèi)部的事件方法或者生命周期方法都可以調(diào)用this.setState來變更,當(dāng)狀態(tài)發(fā)生變化的同時(shí),組件也會(huì)觸發(fā)render函數(shù)進(jìn)行渲染更新。

如果component的某些狀態(tài)需要被改變,并且會(huì)影響到component的render,那么這些狀態(tài)就應(yīng)該用state表示。

例如:一個(gè)購(gòu)物車的component,會(huì)根據(jù)用戶在購(gòu)物車中添加的產(chǎn)品和產(chǎn)品數(shù)量,顯示不同的價(jià)格,那么“總價(jià)”這個(gè)狀態(tài),就應(yīng)該用state表示。

責(zé)任編輯:武曉燕 來源: 簡(jiǎn)書
相關(guān)推薦

2016-08-12 08:49:46

React NativFacebookNative

2024-08-29 08:31:16

2023-03-07 16:12:32

2018-06-13 16:38:33

React Nativ組件Android

2016-08-12 13:22:01

React Nativ環(huán)境搭建創(chuàng)建項(xiàng)目

2016-10-13 19:01:59

React NativUbuntu

2015-07-21 17:23:32

用友IUAP

2021-04-17 18:19:23

FlutterReact Nativ開發(fā)

2019-08-29 09:00:55

開發(fā)Flutter框架

2016-08-12 13:55:06

2023-06-24 17:09:06

React前端

2024-07-08 00:00:07

2021-03-02 09:00:00

開源框架技術(shù)

2015-09-22 09:50:36

FacebookAndroid

2017-09-11 14:35:34

編輯器開發(fā)環(huán)境React

2017-03-09 13:29:04

ReactNative JSPatch

2017-03-21 21:37:06

組件UI測(cè)試架構(gòu)

2024-01-19 09:03:06

ReactTypeScripFlexbox

2024-02-20 01:53:01

ReactFlutter開發(fā)

2025-01-24 08:34:28

CSSWebAndroid
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)