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

相关文章

前端原生js实现拖拽排课效果实例

《前端原生js实现拖拽排课效果实例》:本文主要介绍如何实现一个简单的课程表拖拽功能,通过HTML、CSS和JavaScript的配合,我们实现了课程项的拖拽、放置和显示功能,文中通过实例代码介绍的... 目录1. 效果展示2. 效果分析2.1 关键点2.2 实现方法3. 代码实现3.1 html部分3.2

CSS弹性布局常用设置方式

《CSS弹性布局常用设置方式》文章总结了CSS布局与样式的常用属性和技巧,包括视口单位、弹性盒子布局、浮动元素、背景和边框样式、文本和阴影效果、溢出隐藏、定位以及背景渐变等,通过这些技巧,可以实现复杂... 一、单位元素vm 1vm 为视口的1%vh 视口高的1%vmin 参照长边vmax 参照长边re

CSS3中使用flex和grid实现等高元素布局的示例代码

《CSS3中使用flex和grid实现等高元素布局的示例代码》:本文主要介绍了使用CSS3中的Flexbox和Grid布局实现等高元素布局的方法,通过简单的两列实现、每行放置3列以及全部代码的展示,展示了这两种布局方式的实现细节和效果,详细内容请阅读本文,希望能对你有所帮助... 过往的实现方法是使用浮动加

css渐变色背景|<gradient示例详解

《css渐变色背景|<gradient示例详解》CSS渐变是一种从一种颜色平滑过渡到另一种颜色的效果,可以作为元素的背景,它包括线性渐变、径向渐变和锥形渐变,本文介绍css渐变色背景|<gradien... 使用渐变色作为背景可以直接将渐China编程变色用作元素的背景,可以看做是一种特殊的背景图片。(是作为背

C#比较两个List集合内容是否相同的几种方法

《C#比较两个List集合内容是否相同的几种方法》本文详细介绍了在C#中比较两个List集合内容是否相同的方法,包括非自定义类和自定义类的元素比较,对于非自定义类,可以使用SequenceEqual、... 目录 一、非自定义类的元素比较1. 使用 SequenceEqual 方法(顺序和内容都相等)2.

CSS自定义浏览器滚动条样式完整代码

《CSS自定义浏览器滚动条样式完整代码》:本文主要介绍了如何使用CSS自定义浏览器滚动条的样式,包括隐藏滚动条的角落、设置滚动条的基本样式、轨道样式和滑块样式,并提供了完整的CSS代码示例,通过这些技巧,你可以为你的网站添加个性化的滚动条样式,从而提升用户体验,详细内容请阅读本文,希望能对你有所帮助...

css实现图片旋转功能

《css实现图片旋转功能》:本文主要介绍了四种CSS变换效果:图片旋转90度、水平翻转、垂直翻转,并附带了相应的代码示例,详细内容请阅读本文,希望能对你有所帮助... 一 css实现图片旋转90度.icon{ -moz-transform:rotate(-90deg); -webkit-transfo

vue基于ElementUI动态设置表格高度的3种方法

《vue基于ElementUI动态设置表格高度的3种方法》ElementUI+vue动态设置表格高度的几种方法,抛砖引玉,还有其它方法动态设置表格高度,大家可以开动脑筋... 方法一、css + js的形式这个方法需要在表格外层设置一个div,原理是将表格的高度设置成外层div的高度,所以外层的div需要

对postgresql日期和时间的比较

《对postgresql日期和时间的比较》文章介绍了在数据库中处理日期和时间类型时的一些注意事项,包括如何将字符串转换为日期或时间类型,以及在比较时自动转换的情况,作者建议在使用数据库时,根据具体情况... 目录PostgreSQL日期和时间比较DB里保存到时分秒,需要和年月日比较db里存储date或者ti

四种Flutter子页面向父组件传递数据的方法介绍

《四种Flutter子页面向父组件传递数据的方法介绍》在Flutter中,如果父组件需要调用子组件的方法,可以通过常用的四种方式实现,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录方法 1:使用 GlobalKey 和 State 调用子组件方法方法 2:通过回调函数(Callb