[关闭]
@linux1s1s 2017-03-10T05:59:44.000000Z 字数 3446 阅读 1775

React-Native Primary 基本控件&Fetch/Ajax

React-Native 2017-03


我们接着React-Native Primary Flex布局这篇博客继续往下看基本组件,然后会有个UI和网络协调工作的Demo

RN组件属性

此处输入图片的描述

RN世界里的Fetch网络请求

关于Fetch的详细用法可以参考Promise传统 Ajax 已死,Fetch 永生,这里仅仅给个示例,不再详解。

  1. var REQUEST_URL = 'https://raw.githubusercontent.com/facebook/react-native/master/docs/MoviesExample.json';
  2. fetchData() {
  3. fetch(REQUEST_URL, {
  4. method: 'GET'
  5. })
  6. .then((response) => response.json())
  7. .then((responseData) => {
  8. this.setState({
  9. movies: responseData.movies,
  10. });
  11. })
  12. .catch((error) => {
  13. callback(error);
  14. });
  15. }

这里补充一下Json

此处输入图片的描述

我们从这里的解析可以看到,相比Native的Json解析,这里天然融合了字符串和POJO,而且在操作这个POJO的时候也极其方便。

Flex布局和网络请求实例

关于Flex布局的部分在React-Native Primary Flex布局这篇博客已经有详细说明,这里将Flxe布局和上面讲到的Fetch网络请求融合在一起,做个简单的数据驱动更新UI的小Demo,该Damo参考React Native网络请求及UI展示,有部分改动,特此说明。

  1. import React, {Component} from 'react';
  2. import {AppRegistry, StyleSheet, Image, Text, View} from 'react-native';
  3. var REQUEST_URL = 'https://raw.githubusercontent.com/facebook/react-native/master/docs/MoviesExample.json';
  4. const styles = StyleSheet.create({
  5. /**
  6. * 最外层容器,定义内层容器的布局方向,这里是横向
  7. */
  8. container: {
  9. flex: 1, flexDirection: 'row', justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF'
  10. },
  11. /**
  12. * 内层容器,左边容器,flex:1 和Android的weight相似
  13. */
  14. leftContainer: {
  15. flex: 1
  16. },
  17. /**
  18. * 内层容器,中间容器,flex:1 和Android的weight相似
  19. */
  20. middleContainer: {
  21. flex: 1
  22. },
  23. /**
  24. * 内层容器,右边容器,flex:1 和Android的weight相似
  25. */
  26. rightContainer: {
  27. flex: 1
  28. },
  29. /**
  30. * Item,海报图布局定义
  31. */
  32. thumbnail: {
  33. alignItems: 'center',
  34. width: 100,
  35. height: 80,
  36. marginLeft: 5,
  37. marginRight: 5,
  38. },
  39. /**
  40. * Item,电影名称布局定义
  41. */
  42. title: {
  43. fontSize: 20, marginBottom: 8, textAlign: 'center'
  44. },
  45. /**
  46. * Item,上映年份布局定义
  47. */
  48. year: {
  49. fontSize: 18,
  50. textAlign: 'center'
  51. },
  52. });
  53. /**
  54. * 定义一个组件NwesomeProject,此名字必须与工程名一致
  55. props:(1)大多数组件在创建时要设置各种参数,用于定制的这些参数就叫属性props
  56. (2)比如Image设置source={pic},pic为图片网络或本地路径,比如自定义的属性
  57. (3)与state相比,props是在父组件中指定,并且一经指定不可更改。
  58. state:state是用来监控状态改变,进而自动刷新页面的组件,我们将movies放入state中,表示从网络加载数据,数据返回后会触发setState方法修改movies的内容,这时候对应UI监控state的地方会自动刷新,重新执行,达到UI刷新的目的。
  59. */
  60. class NwesomeProject extends Component {
  61. constructor(props) {
  62. super(props);
  63. this.state = {
  64. movies: null,
  65. };
  66. }
  67. /**
  68. * render方法用于返回组件创建的UI对象,根据state的movies来判断,如果movies没有东西,则调用renderLoadingView方法返回loading界面。
  69. * @returns {XML}
  70. */
  71. render() {
  72. if (!this.state.movies) {
  73. return this.renderLoadingView();
  74. }
  75. /**
  76. * 将网络请求返回的movie传进来,初始化对应的View,Image,Text显示图片,文本信息。
  77. */
  78. return (
  79. <View style={styles.container}>
  80. <View style={styles.leftContainer}>
  81. <Image source={{uri:this.state.movies[0].posters.thumbnail}}
  82. style={styles.thumbnail}/>
  83. <Text style={styles.title}>{this.state.movies[0].title}</Text>
  84. <Text style={styles.year}>{this.state.movies[0].year}</Text>
  85. </View>
  86. <View style={styles.middleContainer}>
  87. <Image source={{uri:this.state.movies[1].posters.thumbnail}}
  88. style={styles.thumbnail}/>
  89. <Text style={styles.title}>{this.state.movies[1].title}</Text>
  90. <Text style={styles.year}>{this.state.movies[1].year}</Text>
  91. </View>
  92. <View style={styles.rightContainer}>
  93. <Image source={{uri:this.state.movies[4].posters.thumbnail}}
  94. style={styles.thumbnail}/>
  95. <Text style={styles.title}>{this.state.movies[4].title}</Text>
  96. <Text style={styles.year}>{this.state.movies[4].year}</Text>
  97. </View>
  98. </View>
  99. );
  100. }
  101. fetchData() {
  102. fetch(REQUEST_URL, {
  103. method: 'GET'
  104. })
  105. .then((response) => response.json())
  106. .then((responseData) => {
  107. this.setState({
  108. movies: responseData.movies,
  109. });
  110. })
  111. .catch((error) => {
  112. callback(error);
  113. });
  114. }
  115. /**
  116. * 组件加载完毕后调用请求网络的方法。
  117. */
  118. componentDidMount() {
  119. this.fetchData();
  120. }
  121. /**
  122. * renderLoadingView方法返回加载中的界面,包括一个View,View中嵌套Text,Text相当于iOS中的label
  123. * @returns {XML}
  124. */
  125. renderLoadingView() {
  126. return (
  127. <View style={styles.container}>
  128. <Text>
  129. 正在加载电影数据......
  130. </Text>
  131. </View>
  132. );
  133. }
  134. }
  135. AppRegistry.registerComponent('NwesomeProject', () => NwesomeProject);

加载结果

此处输入图片的描述

参考:
React Native网络请求及UI展示
React-Native的基本控件属性方法

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注