React Navigation 自认比较好的navigator组件(三)

本文主要是介绍React Navigation 自认比较好的navigator组件(三),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这篇文章主要的内容是自定义的navigator,因为在某些情况下,默认的StackNavigator和TabNavigator无法满足需求(TabNavigator在android环境下默认是基于scrollView的滚动样式,会导致一些第三方组件无法正常运行,并且也不好修改),所以就需要我们按照自己的想法来自定一个navigator。http://www.jianshu.com/p/27288498d819

Custom Navigators

一个导航器可以是一个任何拥有router的React组件,下面是一个简单的例子:

class MyNavigator extends React.Component {static router = MyRouter;render() {const { state, dispatch } = this.props.navigation;const { routes, index } = state;// Figure out what to render based on the navigation state and the router:const Component = MyRouter.getComponentForState(state);// The state of the active child screen can be found at routes[index]let childNavigation = { dispatch, state: routes[index] };// If we want, we can also tinker with the dispatch function here, to limit// or augment our children's actions// Assuming our children want the convenience of calling .navigate() and so on,// we should call addNavigationHelpers to augment our navigation prop:childNavigation = addNavigationHelpers(childNavigation);return <Component navigation={childNavigation} />;}
}

自己模仿示例代码自定义了一个TabBar

/*** Created by Guodada on 2017/3/2.*/
import React from 'react';
import {Button,Platform,ScrollView,StyleSheet,Text,TouchableOpacity,View,Dimensions,TouchableHighlight,} from 'react-native';import {createNavigator,createNavigationContainer,TabRouter,addNavigationHelpers,
} from 'react-navigation';//导入界面
import Home from './Home/Home';
import Find from './Find/Find';
import Me from './Me/Me';
import Contacts from './Contacts/Contacts';const {width,height} = Dimensions.get('window');//设置每个标签
const CustomTabBar = ({navigation
}) => {console.log('TabBar navigation ============'+ navigation.state.index);const {routes} = navigation.state;let selectedIndex = navigation.state.index;let isSelected = false;return (<View style={styles.tabContainer}>{routes.map((route,index) =>{//如果当前选中该index的routerselectedIndex === index?isSelected = true:isSelected = false;//显示不同外观let textColor = isSelected?'blue':'gray';return (<TouchableOpacityonPress={() => {navigation.navigate(route.routeName);}}key={route.routeName}style={styles.tab}><Text style={{color:textColor,fontSize:12}}>{route.routeName}</Text></TouchableOpacity>)})}</View>);
};//设置TabBar
const CustomTabView = ({router,navigation
}) => {console.log('navigation ============>>>>>'+ navigation.state.index);const {routes,index} = navigation.state;const ActiveScreen = router.getComponentForState(navigation.state);return (<View style={styles.container}><ActiveScreennavigation={addNavigationHelpers({...navigation,state:routes[index],})}/><CustomTabBar navigation={navigation}/></View>);
};//自定义导航路由
const CustomTabRouter = TabRouter({Home:{screen:Home,},Me:{screen:Me,},Contacts:{screen:Contacts,},Find:{screen:Find,},},                   {initialRouteName:'Home'}
);//创建自定义TabBar
const CustomTabs = createNavigationContainer(createNavigator(CustomTabRouter)(CustomTabView));const styles = StyleSheet.create({container: {flex:1,justifyContent:'center'},tabContainer: {flexDirection: 'row',height: 48,borderTopWidth:1,borderColor:'lightgray'},tab: {flex: 1,alignItems: 'center',justifyContent: 'flex-end',margin: 4,borderWidth: 1,borderColor: '#ddd',borderRadius: 4,}
});export default CustomTabs;

文末彩蛋,传值方式
从前往后:

 <Button       title='Press me!'onPress={() => navigate('Home_2',{passValue:(value)=> console.log('The value passed by child scene is '+ value),name:'jack'})}color='black'/>

从后往前:

 <Button title='<Back'onPress={()=> {goBack();state.params.passValue('This is the value needs to pass front scene');}}/>

这篇关于React Navigation 自认比较好的navigator组件(三)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/509823

相关文章

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

前端如何通过nginx访问本地端口

《前端如何通过nginx访问本地端口》:本文主要介绍前端如何通过nginx访问本地端口的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、nginx安装1、下载(1)下载地址(2)系统选择(3)版本选择2、安装部署(1)解压(2)配置文件修改(3)启动(4)

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动

HTML input 标签示例详解

《HTMLinput标签示例详解》input标签主要用于接收用户的输入,随type属性值的不同,变换其具体功能,本文通过实例图文并茂的形式给大家介绍HTMLinput标签,感兴趣的朋友一... 目录通用属性输入框单行文本输入框 text密码输入框 password数字输入框 number电子邮件输入编程框

HTML img标签和超链接标签详细介绍

《HTMLimg标签和超链接标签详细介绍》:本文主要介绍了HTML中img标签的使用,包括src属性(指定图片路径)、相对/绝对路径区别、alt替代文本、title提示、宽高控制及边框设置等,详细内容请阅读本文,希望能对你有所帮助... 目录img 标签src 属性alt 属性title 属性width/h

CSS3打造的现代交互式登录界面详细实现过程

《CSS3打造的现代交互式登录界面详细实现过程》本文介绍CSS3和jQuery在登录界面设计中的应用,涵盖动画、选择器、自定义字体及盒模型技术,提升界面美观与交互性,同时优化性能和可访问性,感兴趣的朋... 目录1. css3用户登录界面设计概述1.1 用户界面设计的重要性1.2 CSS3的新特性与优势1.

HTML5 中的<button>标签用法和特征

《HTML5中的<button>标签用法和特征》在HTML5中,button标签用于定义一个可点击的按钮,它是创建交互式网页的重要元素之一,本文将深入解析HTML5中的button标签,详细介绍其属... 目录引言<button> 标签的基本用法<button> 标签的属性typevaluedisabled

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F