react-native入门教程
1、React Native是什么, 能解决什么问题
2、对比其他技术理解为什么使用React Native
3、React Native的环境搭建
4、React Native 的组件,样式
5、一些问题

1)搭建JAVA开发环境
根据操作系统分为x86或x64位的,下载jdk1.8以上的版本,
本机安装时的java版本:jdk-8u45-windows-x64.exe
配置JAVA的环境变量- JAVA_HOME = C:\Program Files\Java\jdk1.8.0_45 。。。。。。。。、、
2)安装Android Studio
本机安装时的Android Studio版本:androidstudio2.1.1
增加Android SDK的环境变量配置
http://developer.android.com/sdk/index.html SDK下载地址
- ANDROID_HOME = D:\Android\sdk
- path 增加 ;%ANDROID_HOME%;%ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools;

3)安装NodeJS开发环境
安装流程参考:http://blog.csdn.net/itpinpai/article/details/481038454)安装Git版本控制
可选择安装,但建议安装,之后用的多。
安装流程参考:http://blog.csdn.net/itpinpai/article/details/48105445
5)安装虚拟机
这个软件需要在官网上注册一个账号,先注册才能下载,官网地址:www.genymotion.com/
6)安装React-native-cli
npm install -g react-native-cli
- npm config set registry https://registry.npm.taobao.org
- npm config set disturl https://npm.taobao.org/dist
如果你安装的模块依赖了 C++ 模块, 需要编译, 肯定会通过 node-gyp 来编译, node-gyp 在第一次编译的时候, 需要依赖 node 源代码, 于是又会去 node dist 下载, 于是大家又会吐槽, 怎么 npm 安装这么慢…
这就是为何要设置disturl
7)第一个React Native程序
测试是否创建成功
cd training
运行 android app
cd training
解决方法:在手机端按菜单键 -> Dev Settings -> Debug server host&port for device -> 在弹出的框中输入pc的ip和端口号, 如10.0.3.2:8081 确认后回到界面, 点击 菜单 -> Reload 重新加载
了解代码结构
-
import模块
类似于java的import和c++的#include,用于引入其他模块
import React, { Component } from 'react'; // 从react-native中批量导入组件 import { AppRegistry, StyleSheet, Text, View } from 'react-native'; -
视图组件声明模块
界面的入口, render函数用于渲染视图, return返回的即为界面显示的内容.其语法规范是JSX
class Training extends Component { render() { return ( <View >...</View> ); } } -
样式配置模块.
此模块配置了视图显示的样式效果.在视图组件模板中的使用方式 给视图添加类似属性
style={styles.container}即可const styles = StyleSheet.create({ container: { ... }, welcome: { ... }, instructions: { ... }, }); -
注册应用的入口.
第一个'ItcastDemo'是项目名称, 第二个ItcastDemo是视图组件名称. 两个名称可以不同。此入口必须要配置。
AppRegistry.registerComponent('training', () => Training);
React Native的特点及需要掌握的知识
- 特点:
- 界面展示(Just the UI), 作为MVC的View层
- 虚拟DOM (Virtual DOM), 特性,在内存中高效更新视图,实现差异化更新
- 数据流 (Data Flow), 单向数据流. 渲染顺序从父节点传递到子节点,如果顶层组件的某个prop改变了,React会递归的向下便利整棵组件树,重新渲染所有使用这个属性的组件。
-
需要的知识
- JSX语法(全程JavaScript XML) ,每一个XML标签都会被JSX转换工具转换成纯Javascript代码
- ES6语法知识.React Native从0.18之后,新建项目默认已经采用了ES6语法.可以参考阮一峰的ES6教程,非常详细。
- css, div, js基础

1)图片组件Image
-
本地图片引用
class Training extends Component { render() { return ( <Image style={styles.pic} source={require('./img/164854172.jpg')}></Image> ); } } -
网络图片的引用
class Training extends Component { render() { return ( <Image resizeMode='contain' style={styles.pic} source={{uri:'https://www.baidu.com/img/bd_logo1.png'}}></Image> ); } }
2)组件的嵌套
- 组件的嵌套
-
class Training extends Component { render() { return ( <View style={styles.container}> <Image resizeMode='contain' style={styles.pic} source={{uri:'https://www.baidu.com/img/bd_logo1.png'}}></Image> <Tip /> </View> ); } } //自定义一个组件 class Tip extends Component { render() { return ( <View> <Text style={{color:'green'}}>我是react-native,我用来跨平台!</Text> < Tip2 /> </View> ); } } //自定义一个子组件 class Tip2 extends Component { render() { return ( <Text style={{color:'red'}}>我是孩子组件!</Text> ); } }
3)组件的属性Props
-
引入组件属性
class MyProp extends Component { //初始化props默认值 static defaultProps = { name:'我是默认值', } //props验证 static propTypes = { name:React.PropTypes.string.isRequired } render() { return ( <Text style={{color:'red'}}>Hello,{this.props.name}!</Text> ); } } class MyProps extends Component { render() { return ( <View style={styles.container}> <MyProp/> <MyProp name='牛逼' /> </View> ); } }
4)组件的状态State
-
引入组件状态
class MyState extends Component { constructor(props){ super(props); this.state = { showText:true, }; //每一秒中对showText进行取反操作。 setInterval(()=>{ this.setState({ showText:!this.state.showText }); },1000); } render() { let display = this.state.showText?this.props.text:''; return ( <Text>{display}</Text> ); } } class MyStates extends Component { render() { return ( <View style={styles.container}> <MyState text='这是啥玩意' /> </View> ); } }
5)文本输入TextInpput组件
-
引入文本输入
/** * 练习文本输入 */ class MyTextInput extends Component{ constructor(props){ super(props); this.state = { text:'' }; } render(){ return( <View style={{padding:10}}> <TextInput style={{height:40}} placeholder='请输入翻译文字' onChangeText={(text)=>this.setState({text})}/> <Text style={{padding:10,fontSize:42}}> {this.state.text.split(' ').map((word)=>word && '#')} </Text> </View> ); } }
6)动画组件Animated
-
引入动画
/** * 动画效果 */ class MyAnimation extends Component{ constructor(props:any){ super(props); this.state = { bounceValue:new Animated.Value(0), }; } render():ReactElement{ return ( <Animated.Image source={{uri:'https://www.baidu.com/img/bd_logo1.png'}} resizeMode='contain' style={{ flex:1, transform:[ {scale:this.state.bounceValue}, ] }}> </Animated.Image> ); } componentDidMount(){ this.state.bounceValue.setValue(1.5); Animated.spring( this.state.bounceValue, { toValue:0.8, friction:1, tension:100 } ).start(); } }
7)完整综合练习实例
-
网络请求,样式,列表
var REQUEST_URL = 'https://raw.githubusercontent.com/facebook/react-native/master/docs/MoviesExample.json'; class SimpleList extends Component{ constructor(props){ super(props); this.state = { movies:null, }; /*this.fetchData = this.fetchData.bind(this);源代码中是由这一句的,但是去除也没有问题。*/ } componentDidMount(){ this.fetchData(); } fetchData(){ //在es6中,只要涉及到this指正,都需要使用bind或者=>来绑this引用 fetch(REQUEST_URL) .then((respose) => respose.json()) .then(function(resposeData){ this.setState({ movies:resposeData.movies, }); }.bind(this)).done(); } render(){ if(!this.state.movies){ return this.renderLoadingView(); } var movie = this.state.movies[1]; return this.renderMovie(movie); } renderLoadingView(){ return ( <View style={styles.container}> <Text> 正在加载电影数据…… </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> ); } }
- 学习react-native的成本比较高。
- 版本更新太快。
- 热部署限制

浙公网安备 33010602011771号