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 如何通过json配置生成查询表单

《Vue3如何通过json配置生成查询表单》本文给大家介绍Vue3如何通过json配置生成查询表单,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录功能实现背景项目代码案例功能实现背景通过vue3实现后台管理项目一定含有表格功能,通常离不开表单

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

Python如何实现高效的文件/目录比较

《Python如何实现高效的文件/目录比较》在系统维护、数据同步或版本控制场景中,我们经常需要比较两个目录的差异,本文将分享一下如何用Python实现高效的文件/目录比较,并灵活处理排除规则,希望对大... 目录案例一:基础目录比较与排除实现案例二:高性能大文件比较案例三:跨平台路径处理案例四:可视化差异报

基于Python Playwright进行前端性能测试的脚本实现

《基于PythonPlaywright进行前端性能测试的脚本实现》在当今Web应用开发中,性能优化是提升用户体验的关键因素之一,本文将介绍如何使用Playwright构建一个自动化性能测试工具,希望... 目录引言工具概述整体架构核心实现解析1. 浏览器初始化2. 性能数据收集3. 资源分析4. 关键性能指

Olingo分析和实践之OData框架核心组件初始化(关键步骤)

《Olingo分析和实践之OData框架核心组件初始化(关键步骤)》ODataSpringBootService通过初始化OData实例和服务元数据,构建框架核心能力与数据模型结构,实现序列化、URI... 目录概述第一步:OData实例创建1.1 OData.newInstance() 详细分析1.1.1

从入门到精通详解LangChain加载HTML内容的全攻略

《从入门到精通详解LangChain加载HTML内容的全攻略》这篇文章主要为大家详细介绍了如何用LangChain优雅地处理HTML内容,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录引言:当大语言模型遇见html一、HTML加载器为什么需要专门的HTML加载器核心加载器对比表二