react-navigation 实战
npm install --save react-navigation
1.测试TabNavigator、StackNavigator和DrawerNavigator

(1)新建HomeScreen.js
/**
* 主页面
*/
import React, { Component } from 'react';
import {
StyleSheet,
View,
Text,
Button,
Image,
} from 'react-native'; // 引入 导航组件
import {
StackNavigator,
TabNavigator,
TabBarBottom,
} from 'react-navigation'; // TabBar 子组件
import TabBarItem from './TabBarItem'; // 引入 其他组件
import MainScreen from './MainPage';
import MineScreen from './MinePage';
import ChatScreen from './ChatScreen'; export default class HomeScreen extends Component {
// 渲染页面
render() {
return (
<Navigator />
)
}
} /**
* TabNavigator Tab选项卡
* TabNavigator(RouteConfigs, TabNavigatorConfig)
* 参数1:表示各个页面路由配置
* 参数2:tab属性配置
*/
// 注册tabs (底部选项卡)
const Tab = TabNavigator(
{
Main:{
screen:MainScreen, // 对应界面名称,可以在其他页面通过这个screen传值和跳转。
navigationOptions:({navigation}) => ({ // 配置TabNavigator的一些属性
tabBarLabel:'首页', // 设置标签栏的title
tabBarIcon:({focused,tintColor}) => ( // 设置标签栏的图标。需要给每个都设置
<TabBarItem
tintColor={tintColor}
focused={focused}
normalImage={require('./image/home.png')}
selectedImage={require('./image/home.png')}
/>
)
}),
}, Mine:{
screen:MineScreen,
navigationOptions:({navigation}) => ({
tabBarLabel:'我的',
tabBarIcon:({focused,tintColor}) => (
<TabBarItem
tintColor={tintColor}
focused={focused}
normalImage={require('./image/mine.png')}
selectedImage={require('./image/mine.png')}
/>
)
}),
},
},
{
tabBarComponent:TabBarBottom, // 导航器 组件
tabBarPosition:'bottom', // 显示在底端,android 默认是显示在页面顶端的
swipeEnabled:false, // 禁止左右滑动
animationEnabled:false, // 切换页面时不显示动画
lazy:true, // 懒加载
tabBarOptions:{
activeTintColor:'#06c1ae', // 文字和图片选中颜色
inactiveTintColor:'#979797', // 文字和图片默认颜色
indicatorStyle: {height: 0}, // android 中TabBar下面会显示一条线,高度设为 0 后就不显示线了
style:{
backgroundColor:'#ffffff', // TabBar 背景色
},
labelStyle: {
fontSize: 12, // 文字大小
},
}
}
); /**
* 注册导航
*/
const Navigator = StackNavigator(
{
Tab:{screen:Tab},
Chat:{screen:ChatScreen},
},
{
initialRouteName:'Tab', // 默认显示页面
navigationOptions:{
// header:null, // 可以设置一些导航的属性,如果隐藏顶部导航栏只要将这个属性设置为null
headerBackTitle: null, // 设置跳转页面左侧返回箭头后面的文字,默认是上一个页面的标题。
headerTitleStyle: {fontSize:18, color:'#666666',alignSelf:'center'}, // 设置alignSelf:'center' 文字居中
headerStyle: {height:48, backgroundColor:'#00BFFF'},
},
mode:'card', // 使用iOS和安卓默认的风格
}
);
(2)新建MainPage.js
/**
* 首页
*/
import React, { Component } from 'react';
import {
Button,
Image,
View,
Text,
StyleSheet,
} from 'react-native'; export default class MainPage extends Component {
static navigationOptions = {
title:'首页', // 顶部标题 }; render() {
const {navigate} = this.props.navigation;
return(
<View style={styles.container}>
<Text onPress={() =>{
navigate('Chat',{user:'Sybil'})
}}>点击跳转到'聊天页面'</Text>
</View>
);
} _skip() {
/**
* 页面跳转并传值
* 参数1:页面名称
* 参数2:传的值
* <Text onPress={this._skip.bind(this)}>点击跳转到'我的页面'</Text>
*/
// this.props.navigation.navigate('Chat',{user:'Sybil'});
}
} // 层叠样式表
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});
(3)新建MinePage.js
/**
* 我的
*/
import React, {Component} from 'react';
import {
Button,
Image,
View,
Text,
StyleSheet,
} from 'react-native'; import {
DrawerNavigator
} from 'react-navigation'; // 引入 侧滑菜单组件 (通知页)
import MyNotificationsScreen from './MyNotificationsScreen'; // 定义 我的组件
class MinePage extends Component{
// 定义抽屉子组件样式
static navigationOptions = {
title:'我的',
drawerLabel: '我的',
drawerIcon: ({ tintColor }) => (
<Image
source={require('./image/chat.png')}
style={[styles.tabIcon, {tintColor: tintColor}]}
/>
),
}; // 组件加载完成
componentDidMount() {
// 获取传值 {this.props.navigation.state.params.info}
// const {params} = this.props.navigation.state;
// const user = params.user;
// alert(user);
} render(){;
return(
<View style={styles.container}>
<Text style={{padding:20}}>Sybil</Text>
<Button
style={{padding:20}}
onPress={() => this.props.navigation.navigate('DrawerOpen')}
title="点击打开侧滑菜单"
/>
</View>
);
}
} const styles = StyleSheet.create({
container:{
flex:1,
backgroundColor:'#fff',
},
tabIcon: {
width: 16,
height: 16,
},
}); /**
* 注册抽屉 (侧滑菜单)
* DrawerNavigator(RouteConfigs, DrawerNavigatorConfig)
* 参数1:抽屉包含的子组件
* 参数2:抽屉的样式
*/
const MyDrawerNavigator = DrawerNavigator(
{
Mine: {
screen: MinePage,
},
Notifications: {
screen: MyNotificationsScreen,
},
},
{
drawerWidth: 200, // 抽屉宽
drawerPosition: 'left', // 抽屉在左边还是右边
// contentComponent: CustomDrawerContentComponent, // 自定义抽屉组件
contentOptions: {
initialRouteName: MinePage, // 默认页面组件
activeTintColor: '#008AC9', // 选中文字颜色
activeBackgroundColor: '#f5f5f5', // 选中背景颜色
inactiveTintColor: '#000', // 未选中文字颜色
inactiveBackgroundColor: '#fff', // 未选中背景颜色
style: { // 样式 }
}
}
); // 默认向外暴露 '我的抽屉' 组件
export default MyDrawerNavigator;
(4)编写TabBarItem.js
/**
* TabBarItem 组件
*/
import React,{Component} from 'react';
import {Image} from 'react-native'; export default class TabBarItem extends Component {
render() {
return(
<Image
source={this.props.focused ? this.props.selectedImage : this.props.normalImage}
style={{tintColor:this.props.tintColor,width:25,height:25 }}
/>
)
}
}
(5)编写ChatScreen.js
/**
* 聊天页
*/
import React, { Component } from 'react';
import {
Button,
Image,
View,
Text
} from 'react-native'; export default class ChatScreen extends Component {
static navigationOptions = {
title:'聊天',
}; render() {
const {params} = this.props.navigation.state;
return (
<View style={{backgroundColor:'#fff',flex:1}}>
<Text style={{padding:20}}>Chat with {params.user}</Text> </View> );
}
}
(6)编写MyNotificationsScreen.js
/**
* 侧滑菜单
* 通知页
*/
import React, { Component } from 'react';
import {
StyleSheet,
View,
Text,
Button,
Image
} from 'react-native'; // 定义 通知组件
export default class MyNotificationsScreen extends Component {
// 定义抽屉子组件样式
static navigationOptions = {
title:'通知',
drawerLabel: '通知',
drawerIcon: ({ tintColor }) => (
<Image
source={require('./image/notif.png')}
style={[styles.tabIcon, {tintColor: tintColor}]}
/>
),
}; render() {
return (
<View style={styles.container}>
<Button
style={{padding:20}}
onPress={() => this.props.navigation.navigate('DrawerOpen')}
title="点击打开侧滑菜单"
/>
<Button
onPress={() => this.props.navigation.goBack()}
title="返回我的界面"
/>
</View>
);
}
} const styles = StyleSheet.create({
container: {
backgroundColor:'#fff',
},
tabIcon: {
width: 16,
height: 16,
},
});
效果图





react-navigation 实战的更多相关文章
- RN 实战 & React Native 实战
RN 实战 & React Native 实战 https://abc.xgqfrms.xyz/react-native-docs/ 0.59 https://github.com/xgqfr ...
- react-native 学习 ----- React Navigation
很久没有的登陆博客园了,密码都是找回的,从当年的大学生已经正常的走上了程序员的道路,看到之前发的博客还是写的android,现在自己已经在使用了react-native了. 大学毕业了,做了java后 ...
- react-native导航器 react navigation 介绍
开发环境搭建好之后,想要进一步了解react-native,可以先从react-native官网上的电影列表案例入手: https://reactnative.cn/docs/0.51/sample- ...
- React Navigation & React Native & React Native Navigation
React Navigation & React Native & React Native Navigation React Navigation https://facebook. ...
- [RN] 04 - React Navigation
react-navigation和react-router的对比: 支持的平台: react-navigation: react-native react-router: react-native.r ...
- React Native常用组件之TabBarIOS、TabBarIOS.Item组件、Navigator组件、NavigatorIOS组件、React Navigation第三方
以下内容为老版本React Native,faceBook已经有了新的导航组件,请移步其他博客参考>>[我是传送门] 参考资料:React Navigation react-native ...
- React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发
React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发 2016/09/23 | React Native技术文章 | Sky丶清| 4 条评论 | 1 ...
- React-native 导航插件React Navigation 4.x的使用
React-native 导航插件React Navigation 4.x的使用 文档 英文水平可以的话,建议直接阅读英文文档 简单使用介绍 安装插件 yarn add react-navigatio ...
- [RN] React Navigation 使用中遇到的显示 问题 汇总
React Navigation 使用中遇到的显示 问题 汇总 https://www.jianshu.com/p/8b1f18affc5d
- react navigation goBack()返回到任意页面(不集成redux) 一
方案一: 一.适用场景:在app端开发的时候,相反回到某一个页面的时候保持跳转页面的所有状态不更新,也就是说不触发新的生命周期. 例如:A——>B——>C——>D 要想从D页面直接返 ...
随机推荐
- Idea maven远程调试(pom配置tomcat)
服务器端,maven内置tomcat启动命令:mvnDebug clean tomcat7:run -Dmaven.test.skip=true 服务器端:配置(vim或者文件模式打开mvnDebug ...
- 毕业之后de经历
毕业之后 2016年7月,我大学毕业了.7月3号到7月6号,我陆续用我的小行李箱,在半夜12点左右,把我的生活用品拉出宿舍,大汗淋漓之后,我就在晚上12点多,找个奶茶店买一杯芒果冰沙.白天要去厦门的一 ...
- 前端 CSS 盒子模型
盒模型的概念 在CSS中,"box model"这一术语是用来设计和布局时使用,然后在网页中基本上都会显示一些方方正正的盒子.我们称为这种盒子叫盒模型. 盒模型有两种:标准模型和I ...
- 转 jvisualvm 工具使用 https://www.cnblogs.com/kongzhongqijing/articles/3625340.html
VisualVM 是Netbeans的profile子项目,已在JDK6.0 update 7 中自带(java启动时不需要特定参数,监控工具在bin/jvisualvm.exe). https:// ...
- 创建Maven项目时,出现系列的错误提示的修改方法
1.创建Maven项目成功之后,需要修改一些配置, (1).java版本改为“本系统中java的版本号” 问题一:(2).Dynamic Web Module的version要改为2.5以上,然而本人 ...
- Spring之使用注解实例化Bean并注入属性
1.准备工作 (1)导入jar包 除了上篇文章使用到的基本jar包外,还得加入aop的jar包,所有jar包如下 所需jar包 (2)配置xml <?xml version="1.0& ...
- 《死磕 Elasticsearch 方法论》:普通程序员高效精进的 10 大狠招!(完整版)
原文:<死磕 Elasticsearch 方法论>:普通程序员高效精进的 10 大狠招!(完整版) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链 ...
- 图片,word,Excel等附件上传
@ResponseBody @RequestMapping("/upload") public String upload(HttpServletRequest request, ...
- moongoose对象无法新增删除属性
昨天用nodes中的moongoose去查询一个结果遇到一个大坑,这个坑貌似用moongoose可能会遇到.背景是这样的,我在nodejs中去查询document,得到的可以看作是一个对象list.在 ...
- JavaScript ES6 Promise对象
说明 Node.js中,以异步(Async)回调著称,使用了异步,提高了程序的执行效率,但是,代码可读性较差的. 假如有几个异步操作,后一个操作需要前一个操作的执行完毕之后返回的数据才能执行下去,如果 ...