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

相关文章

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行

前端下载文件时如何后端返回的文件流一些常见方法

《前端下载文件时如何后端返回的文件流一些常见方法》:本文主要介绍前端下载文件时如何后端返回的文件流一些常见方法,包括使用Blob和URL.createObjectURL创建下载链接,以及处理带有C... 目录1. 使用 Blob 和 URL.createObjectURL 创建下载链接例子:使用 Blob

Vuex Actions多参数传递的解决方案

《VuexActions多参数传递的解决方案》在Vuex中,actions的设计默认只支持单个参数传递,这有时会限制我们的使用场景,下面我将详细介绍几种处理多参数传递的解决方案,从基础到高级,... 目录一、对象封装法(推荐)二、参数解构法三、柯里化函数法四、Payload 工厂函数五、TypeScript

SpringQuartz定时任务核心组件JobDetail与Trigger配置

《SpringQuartz定时任务核心组件JobDetail与Trigger配置》Spring框架与Quartz调度器的集成提供了强大而灵活的定时任务解决方案,本文主要介绍了SpringQuartz定... 目录引言一、Spring Quartz基础架构1.1 核心组件概述1.2 Spring集成优势二、J

Vue3使用router,params传参为空问题

《Vue3使用router,params传参为空问题》:本文主要介绍Vue3使用router,params传参为空问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录vue3使用China编程router,params传参为空1.使用query方式传参2.使用 Histo

CSS Padding 和 Margin 区别全解析

《CSSPadding和Margin区别全解析》CSS中的padding和margin是两个非常基础且重要的属性,它们用于控制元素周围的空白区域,本文将详细介绍padding和... 目录css Padding 和 Margin 全解析1. Padding: 内边距2. Margin: 外边距3. Padd

CSS will-change 属性示例详解

《CSSwill-change属性示例详解》will-change是一个CSS属性,用于告诉浏览器某个元素在未来可能会发生哪些变化,本文给大家介绍CSSwill-change属性详解,感... will-change 是一个 css 属性,用于告诉浏览器某个元素在未来可能会发生哪些变化。这可以帮助浏览器优化

CSS去除a标签的下划线的几种方法

《CSS去除a标签的下划线的几种方法》本文给大家分享在CSS中,去除a标签(超链接)的下划线的几种方法,本文给大家介绍的非常详细,感兴趣的朋友一起看看吧... 在 css 中,去除a标签(超链接)的下划线主要有以下几种方法:使用text-decoration属性通用选择器设置:使用a标签选择器,将tex