最近对React Native比较有兴趣,简单把官网的入门例子做了一下,发现了很多坑,特别是watchman版本过低的问题,导致总是运行不起来。所以 这里特别下载了最新的watchman,进行了源码编译。希望本文对刚学习的新人有用。

安装Rect Native

安装必要的依赖watchman

cd watchman-4.5.0
./autogen.sh
./configure --prefix=/usr/local/Cellar/watchman/2.9.8 --with-pcre
make
make install

安装Rect Native开发工具

npm install -g react-native-cli
react-native init <APP_NAME>

IOS版本的项目文件用XCODE打开<APP_NAME>/ios/<APP_NAME>.xcodeproj,运行⌘+R即可

此时通过修改index.ios.js 运行⌘+R查看视图变化

模拟数据

通常在index.ios.js或index.android.js 顶部增加

var MOCKED_MOVIES_DATA = [{title: 'Title', year: '2015', posters: {thumbnail: 'http://i.imgur.com/UePbdph.jpg'}},
];

修改代码,增加Image组件支持

import React, {AppRegistry,Component,Image,StyleSheet,Text,View
} from 'react-native';

修改render

render() {var movie = MOCKED_MOVIES_DATA[0];return (<View style={styles.container}><Text>{movie.title}</Text><Text>{movie.year}</Text><Image source={{uri: movie.posters.thumbnail}}style={styles.thumbnail} /></View>);}

更新样式代码

const styles = StyleSheet.create({container: {flex: 1,justifyContent: 'center',alignItems: 'center',backgroundColor: '#F5FCFF',},thumbnail: {width: 53,height: 81,}
});

Xcode中⌘+R重新载入,即看到视图已载入我们的模拟数据。

重新布局

使用FlexBox布局
改变结构代码

render() {var movie = MOCKED_MOVIES_DATA[0];return (<View style={styles.container}><Image source={{uri: movie.posters.thumbnail}}style={styles.thumbnail} /><View style={styles.rightContainer}><Text style={styles.title}>{movie.title}</Text><Text style={styles.year}>{movie.year}</Text></View></View>);}

更新样式

const styles = StyleSheet.create({container: {flex: 1,flexDirection: 'row',justifyContent: 'center',alignItems: 'center',backgroundColor: '#F5FCFF',},rightContainer: {flex: 1,},title: {fontSize: 20,marginBottom: 8,textAlign: 'center',},year: {textAlign: 'center',},thumbnail: {width: 53,height: 81,}
});

获取真实的数据

添加API数据地址

var REQUEST_URL = 'https://raw.githubusercontent.com/facebook/react-native/master/docs/MoviesExample.json';

完整代码

'use strict';import React, {AppRegistry,Component,Image,StyleSheet,Text,View
} from 'react-native';var REQUEST_URL = 'https://raw.githubusercontent.com/facebook/react-native/master/docs/MoviesExample.json';class FaceMash extends Component {constructor(props){super(props);this.state = {movies: null,};}componentDidMount(){this.fetchData();}fetchData(){fetch(REQUEST_URL).then((response)=>response.json()).then((responseData)=>{this.setState({movies: responseData.movies,});}).done();}render() {if(!this.state.movies){return this.renderLoadingView();}var movie = this.state.movies[0];return this.renderMovie(movie);}renderLoadingView(){return (<View style={styles.container}><Text>Loading movies...</Text></View>);}renderMovie(movie){return (<View style={styles.container}><Image source={{uri: movie.posters.thumbnail}}style={styles.thumbnail} /><View style={styles.rightContainer}><Text style={styles.title}>{movie.title}</Text><Text style={styles.year}>{movie.year}</Text></View></View>);}
}const styles = StyleSheet.create({container: {flex: 1,flexDirection: 'row',justifyContent: 'center',alignItems: 'center',backgroundColor: '#F5FCFF',},rightContainer: {flex: 1,},title: {fontSize: 20,marginBottom: 8,textAlign: 'center',},year: {textAlign: 'center',},thumbnail: {width: 53,height: 81,}
});AppRegistry.registerComponent('FaceMash', () => FaceMash);

显示列表

/*** Sample React Native App* https://github.com/facebook/react-native*/
'use strict';import React, {AppRegistry,Component,Image,ListView,StyleSheet,Text,View
} from 'react-native';var API_KEY='7waqfqbprs7pajbz28mqf6vz';
var API_URL = 'http://api.rottentomatoes.com/api/public/v1.0/lists/movies/in_theaters.json';
var PAGE_SIZE = 25;
var PARAMS = '?apikey='+API_KEY+'&page_limit='+PAGE_SIZE;
var REQUEST_URL = API_URL+PARAMS;class FaceMash extends Component {constructor(props){super(props);this.state = {dataSource: new ListView.DataSource({rowHasChanged: (row1, row2) => row1 !== row2,}),loaded: false,};}componentDidMount(){this.fetchData();}fetchData(){fetch(REQUEST_URL).then((response)=>response.json()).then((responseData)=>{this.setState({dataSource: this.state.dataSource.cloneWithRows(responseData.movies),loaded: true,});}).done();}render() {if(!this.state.loaded){return this.renderLoadingView();}return (<ListViewdataSource={this.state.dataSource}renderRow={this.renderMovie}style={styles.listView}></ListView>);}renderLoadingView(){return (<View style={styles.container}><Text>Loading movies...</Text></View>);}renderMovie(movie){return (<View style={styles.container}><Image source={{uri: movie.posters.thumbnail}}style={styles.thumbnail} /><View style={styles.rightContainer}><Text style={styles.title}>{movie.title}</Text><Text style={styles.year}>{movie.year}</Text></View></View>);}
}const styles = StyleSheet.create({container: {flex: 1,flexDirection: 'row',justifyContent: 'center',alignItems: 'center',backgroundColor: '#F5FCFF',},listView: {paddingTop: 20,backgroundColor: '#F5FCFF',},rightContainer: {flex: 1,},title: {fontSize: 20,marginBottom: 8,textAlign: 'center',},year: {textAlign: 'center',},thumbnail: {width: 53,height: 81,}
});AppRegistry.registerComponent('FaceMash', () => FaceMash);

React Native 0.20官方入门教程相关推荐

  1. 用TypeScript来写React官方入门教程 .tsx后缀文件,同时入门typescript和React

    用TypeScript来写React官方入门教程 .tsx后缀文件,同时入门typescript和React 1. 项目说明: 这是React官网上那个下井字棋的入门教程,但是我把它换了typesci ...

  2. 苹果收购英特尔手机芯片业务;西门子将在华建立 5G 研发中心;React Native 0.60.4 发布 | 极客头条...

    快来收听极客头条音频版吧,智能播报由标贝科技提供技术支持. 「CSDN 极客头条」,是从 CSDN 网站延伸至官方微信公众号的特别栏目,专注于一天业界事报道.风里雨里,我们将每天为朋友们,播报最新鲜有 ...

  3. Unity游戏开发官方入门教程:飞机大战(六)——创建子弹

    Unity版本:Unity 2018.2.14f1 原视频链接:https://unity3d.com/cn/learn/tutorials/s/space-shooter-tutorial 教程目录 ...

  4. 余承东吐槽苹果续航;微软 IE 浏览器被曝漏洞;React Native 0.61.0 发布 | 极客头条...

    快来收听极客头条音频版吧,智能播报由标贝科技提供技术支持. 「CSDN 极客头条」,是从 CSDN 网站延伸至官方微信公众号的特别栏目,专注于一天业界事报道.风里雨里,我们将每天为朋友们,播报最新鲜有 ...

  5. AFNnetworking快速教程,官方入门教程译

    AFNnetworking快速教程,官方入门教程译 分类: IOS2013-12-15 20:29 12489人阅读 评论(5) 收藏 举报 afnetworkingjsonios入门教程快速教程 A ...

  6. 9月27日科技资讯|余承东吐槽苹果续航;贾扬清担任阿里巴巴开源技术委员会负责人;React Native 0.61.0 发布

    「CSDN 极客头条」,是从 CSDN 网站延伸至官方微信公众号的特别栏目,专注于一天业界事报道.风里雨里,我们将每天为朋友们,播报最新鲜有料的新闻资讯,让所有技术人,时刻紧跟业界潮流. 整理 | 胡 ...

  7. 第三章 Python Kivy 学习 -- Kivy官方入门教程Pong Game

    系列文章目录 第一章 Python Kivy 学习 – Kivy介绍及环境安装 第二章 Python Kivy 学习 – Kivy项目开发原理(待编辑) 第三章 Python Kivy 学习 – Ki ...

  8. Unity游戏开发官方入门教程:飞机大战(五)——实现飞船控制脚本

    Unity版本:Unity 2018.2.14f1 原视频链接:https://unity3d.com/cn/learn/tutorials/s/space-shooter-tutorial 教程目录 ...

  9. React Native 0.59.0 发布,使用 React 编写原生应用

    React Native 0.59.0 发布了.React Native 使开发者只使用 JavaScript 也能编写原生移动应用. 新版更新亮点: React Hooks React Native ...

最新文章

  1. 基于C++实现线程池加速
  2. python【数据结构与算法】Queue,LifoQueue,PriorityQueue
  3. oracle中 游标实例
  4. des vue 双倍长 解密_[转]单倍长密钥加密和双倍长密钥加密,银联直联终端62域难点详解...
  5. 【DND图形库】一、简介与环境配置
  6. 【Ubuntu】通过虚拟机安装系统( ubuntu )
  7. 【人脸对齐-Landmarks】人脸关键点检测方法及评测汇总
  8. 枚举枚举和修改“最终静态”字段的方法
  9. 团队作业3——需求改进系统设计
  10. 暴力技术(一)——BFS广(宽)度优先搜索
  11. 请听一个故事------gt;百度员工离职总结:如何做个好员工
  12. mysql where varchar_MySQL数据库之MySQL索引使用:字段为varchar类型时,条件要使用''包起来...
  13. SQL Server数据挖掘简介
  14. 保护眼睛——设置WIN7和XP 窗体、Chrome、IE网页背景颜色(zz)
  15. RPG多人回合制游戏战斗框架设计《一:基本战斗流程》
  16. 王者荣耀微信一区的服务器没了,为什么王者荣耀微信区和QQ区玩起来像两个游戏?...
  17. 原生js实现【longPressKey】长按键盘任意键(或组合键)3秒触发自定义事件(以Pause/Break键为例)
  18. Norgen提取试剂盒丨血浆/血清循环和核外RNA提取试剂盒
  19. VS2010 混合模式程序集是针对v1.1.4322版的运行时生成的 在没有配置其他信息的情况下 无法再4.0运行中
  20. tf.keras.metrics.Accuracy;tf.keras.metrics.Precision;tf.keras.metrics.Recall

热门文章

  1. Python UnicodeEncodeError: ‘gbk‘ codec can‘t encode character 解决方法
  2. 关于出现Not an editor command: Bundle ‘**/*.vim‘的解决方案【转】
  3. 解决matplotlib中文显示问题
  4. 解决springmvc加载JS,CSS等文件问题【转】
  5. ie不兼容的几个js问题及解决办法
  6. win7 32位操作系统 ie8浏览器问题解决办法
  7. 如何恢复初始git提交?
  8. mysql5.7 解压版 中文乱码_MySQL 5.7解压版安装、卸载及乱码问题的图文解决方法...
  9. ros中使用boost::thread多线程boost::bind绑定参数,多线程发送topic
  10. dpkg: 依赖关系问题使得 linux-headers-5.7.19-050719-generic 的配置工作不能继续