React-Native实战项目-导航器篇(一)
前言:官方文档已经看了一遍,但印象不是很深,于是在mooc上找了个实战学习项目做一做。
本篇目录:
基础导航练习√
1.ReactNavigation之createStackNavigator导航器案例练习
相关资料:
练习链接:http://www.devio.org/2018/12/24/createStackNavigator/
React Navigationan官网:https://reactnavigation.org/docs/zh-Hans/getting-started.html
1. 初始化react native项目
react-native init react_navigation_demo
2. 在项目目录下安装reactnavigation这个包
yarn add react-navigation
3. 安装依赖
yarn add react-native-reanimated react-native-gesture-handler react-native-screens
4. 版本更新后,此步骤忽略。
5. 新建并配置路由文件navigator/navigators.js

6. 新建页面文件夹Page,构建HomePage.js,Page1.js,Page2.js,Page3.js,Page4.js,Page5.js等页面
HomePage.js
import React,{Component} from 'react';
import {Button ,View,Text,StyleSheet} from 'react-native';
export default class HomePage extends Component{
render(){
const {navigation}=this.props; //获取navigation
return(
<View style={styles.container}>
<Text style={styles.welcome}>Welcome To HomePage</Text>
<Button title={'Go to Page1'} onPress={()=>{
navigation.navigate('Page1',{name:'动态的'});//跳转页面,并且允许传递参数
}} />
<Button title={'Go to Page2'} onPress={()=>{
navigation.navigate('Page2');
}} />
<Button title={'Go to Page3'} onPress={()=>{
navigation.navigate('Page3',{name:'动态的'});
}} />
</View>
)
}
}
const styles=StyleSheet.create({
container:{
flex:1,
},
welcome:{
fontSize:20,
textAlign:'center',
margin:10,
}
});
Page1.js
import React from 'react';
import {Button,View,Text,StyleSheet} from 'react-native'; export default class Page1 extends React.Component{
render(){
const {navigation}=this.props;
return(
<View style={styles.container}>
<Text style={styles.welcome}>Welcome To Page1</Text>
<Button
title={'Go Back'}
onPress={()=>{
navigation.goBack();
}}
/>
<Button
title={'Go TO Page4'}
onPress={()=>{
navigation.navigate('Page4');
}}
/>
</View>
)
}
} const styles=StyleSheet.create({
container:{
flex:1,
},
welcome:{
fontSize:20,
textAlign:'center',
margin:10,
}
});
Page2.js
import React from 'react';
import {View,Text,StyleSheet} from 'react-native'; export default class Page2 extends React.Component{
render(){
const {navigation}=this.props;
return(
<View style={styles.container}>
<Text style={styles.welcome}>Welcome To Page2</Text>
</View>
)
}
}
const styles=StyleSheet.create({
container:{
flex:1,
},
welcome:{
fontSize:20,
textAlign:'center',
margin:10,
}
});
7. 修改根目录下的APP.js,将路由导入
import React from 'react';
import AppContainer from './navigation/navigators.js'//导入路由文件 export default class App extends React.Component {
render() {
return <AppContainer />;
}
}
8. 启动连接虚拟手机,用react-native run-android编译运行代码

2.ReactNavigation之createBottomTabNavigator和createMaterialTopTabNavigator导航器案例练习
·导入createBottomTabNavigator和createMaterialTopTabNavigator导航器及矢量图标库
import {createBottomTabNavigator,createMaterialTopTabNavigator} from 'react-navigation-tabs';//底部导航及头部导航器
import Ionicons from 'react-native-vector-icons/Ionicons';//矢量图标库
import MaterialIcons from 'react-native-vector-icons/MaterialIcons';//矢量图标库

·编写代码
const AppBottomNavigator =createBottomTabNavigator({
Page1:{
screen:Page1,
navigationOptions:{
tabBarLabel:'最热',
tabBarIcon:({tinColor,focused})=>(
<Ionicons
name={"ios-home"}
size={26}
style={{color:tinColor}}
/>
)
}
},
Page2:{
screen:Page2,
navigationOptions:{
tabBarLabel:'趋势',
tabBarIcon:({tinColor,focused})=>(
<Ionicons
name={"ios-people"}
size={26}
style={{color:tinColor}}
/>
)
}
},
Page3:{
screen:Page3,
navigationOptions:{
tabBarLabel:'收藏',
tabBarIcon:({tinColor,focused})=>(
<Ionicons
name={"ios-chatboxes"}
size={26}
style={{color:tinColor}}
/>
)
}
},
Page4:{
screen:Page4,
navigationOptions:{
tabBarLabel:'我的',
tabBarIcon:({tinColor,focused})=>(
<Ionicons
name={"ios-car"}
size={26}
style={{color:tinColor}}
/>
)
}
},
},{
tabBarOptions:{
activeTintColor:Platform.OS==='ios'?'#e91e63':'fff',
}
});
const AppTopNavigator =createMaterialTopTabNavigator({
Page1:{
screen:Page1,
navigationOptions:{
tabBarLabel:'ALL'//顶部导航项
}
},
Page2:{
screen:Page2,
navigationOptions:{
tabBarLabel:'IOS'//顶部导航项
}
},
Page3:{
screen:Page3,
navigationOptions:{
tabBarLabel:'React'//顶部导航项
}
},
Page4:{
screen:Page4,
navigationOptions:{
tabBarLabel:'React Native'//顶部导航项
}
},
Page5:{
screen:Page5,
navigationOptions:{
tabBarLabel:'TI实验室'//顶部导航项
}
},
},{
tabBarOptions:{
tabStyle:{minWidth:50,},//顶部导航项的最小宽
upperCaseLabel:false,//是否使标签大写,默认为true
scrollEnabled:true,//允许滑动切换标签
style:{
backgroundColor:"#678" //TabBar的背景色
},
indicatorStyle:{
height:2,
backgroundColor:"white",
},//标签指示器样式
labelStyle:{
fontSize:13,
marginTop:6,
marginBottom:6,
},//文字样式
}
});
演示效果:

3.ReactNavigation之createDrawerNavigator导航器案例练习(!存在bug)
·导入createDrawerNavigator导航器

编写代码
const AppDrawerNavigator=createDrawerNavigator({
Page4:{
screen:Page4,
navigationOptions:{
drawerLabel:"Page4",
drawerIcon:({tintColor})=>(
<MaterialIcons
name={'drafts'}
size={24}
style={tintColor}
/>
)
}
},
Page5:{
screen:Page5,
navigationOptions:{
drawerLabel:"Page5",
drawerIcon:({tintColor})=>(
<MaterialIcons
name={'move-to-inbox'}
size={24}
style={tintColor}
/>
)
}
},
},{
initialRouteName:'Page4',
contentOptions:{
activeTintColor:'#e91e63'
},
contentComponent:(props)=>(
<ScrollView
style={{backgroundColor:'#789',flex:1}}
>
<SafeAreaView
forceInset={{top:'always',horizontal:'never'}}
>
<DrawerItems {...props}/>
</SafeAreaView>
</ScrollView>
)
});//存在问题,需要后期调整
4.ReactNavigation之createSwitchNavigator导航器案例练习
·该导航器主要与登录验证有关,在后面的章节会具体介绍。
React-Native实战项目-导航器篇(一)的更多相关文章
- 0、手把手教React Native实战之开山篇
##作者简介 东方耀 Android开发 RN技术 facebook github android ios 原生开发 react reactjs nodejs 前端 ...
- 手把手教你React Native 实战之开山篇《一》
先说一下我为什么学习RN 18年3月29号,随着自己内心的欲望和冲动,任务交接了一下,正式离开一家医疗公司.第二天就入职了这之前已经找好的公司,由于自己对代码浓厚的热情,自己终于也不再带团队.正好有充 ...
- React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发
React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发 2016/09/23 | React Native技术文章 | Sky丶清| 4 条评论 | 1 ...
- RN 实战 & React Native 实战
RN 实战 & React Native 实战 https://abc.xgqfrms.xyz/react-native-docs/ 0.59 https://github.com/xgqfr ...
- React Native开源项目案例
(六).React Native开源项目: 1.Pober Wong_17童鞋为gank.io做的纯React Native项目,开源地址:https://github.com/Bob1993/Rea ...
- React Native 开源项目汇总
最近闲来无事,学习了React Native开发Android APP,自我感觉RN APP的效果和Native APP比还是蛮不错,以下是找到的一些优秀源码,仅供学习参考... React Nati ...
- React Native 之 项目实战(一)
前言 本文有配套视频,可以酌情观看. 文中内容因各人理解不同,可能会有所偏差,欢迎朋友们联系我. 文中所有内容仅供学习交流之用,不可用于商业用途,如因此引起的相关法律法规责任,与我无关. 如文中内容对 ...
- React Native之使用导航器跳转页面(react-navigation)
react-navigation是一个导航库,要使用react-navigation来实现跳转页面,首先得在项目中安装此库,由于Yarn是Facebook提供的替代npm的工具,可以加速node模块的 ...
- 【React Native 实战】商品分类
1.前言 商品分类是各种app常见的一种操作,一般都是左右两栏构成,左边栏是商品的分类,右边栏是商品的展示,同时左右两栏都可以滑动.今天我们就用React Native来实现这种效果. 实现内容:1) ...
随机推荐
- 通用的规则匹配算法(原创)(java+.net)
1.java里可以使用Spring的 Spel或者Google的Aviator 如果使用 Aviator 则添加以下依赖 <dependency> <groupId>com.g ...
- C语言获取当前时间
#include <stdio.h> #include <time.h> void main () { time_t rawtime; struct tm * timeinfo ...
- 洛谷P3830 随机树(SHOI2012)概率期望DP
题意:中文题,按照题目要求的二叉树生成方式,问(1)叶平均深度 (2)树平均深度 解法:这道题看完题之后完全没头绪,无奈看题解果然不是我能想到的qwq.题解参考https://blog.csdn.ne ...
- 清理maven缓存
原文:https://blog.csdn.net/viplisong/article/details/82963989maven下载失败后会缓存文件,可能导致下次下载失败.通过以下两步清理 1.cd ...
- Codeforces 958C3 - Encryption (hard) 区间dp+抽屉原理
转自:http://www.cnblogs.com/widsom/p/8863005.html 题目大意: 比起Encryption 中级版,把n的范围扩大到 500000,k,p范围都在100以内, ...
- 51nod 1253:Kundu and Tree(组合数学)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1253 所有的三元组的可能情况数有ans0=C(n,3).然后 ...
- vue项目中echarts使用渐变效果报错echarts is not defined
解决办法:在当前单组件中在引用一次
- 【RabbitMQ】Concurrency、Prefetch、exclusive
分布式消息中间件 RabbitMQ是用Erlang语言编写的分布式消息中间件,常常用在大型网站中作为消息队列来使用,主要目的是各个子系统之间的解耦和异步处理.消息中间件的基本模型是典型的生产者-消费者 ...
- Pangu and Stones HihoCoder - 1636 区间DP
Pangu and Stones HihoCoder - 1636 题意 给你\(n\)堆石子,每次只能合成\(x\)堆石子\((x\in[L, R])\),问把所有石子合成一堆的最小花费. 思路 和 ...
- 【HDOJ6604】Blow up the city(支配树)
题意:给定一个n点m边的DAG,将只有入边的点称为周驿东点 q次询问,每次给定a,b两点,询问删去某个点x和其相连的所有边,能使a,b至少其中之一不能到达任何周驿东点的x的个数 n,q<=1e5 ...