React Navigation 自认比较好的navigator组件(三)
来源:互联网 发布:数控线切割编程3b代码 编辑:程序博客网 时间:2024/05/17 04:47
这篇文章主要的内容是自定义的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的router selectedIndex === index?isSelected = true:isSelected = false; //显示不同外观 let textColor = isSelected?'blue':'gray'; return ( <TouchableOpacity onPress={() => { navigation.navigate(route.routeName); }} key={route.routeName} style={styles.tab} > <Text style={{color:textColor,fontSize:12}}>{route.routeName}</Text> </TouchableOpacity> ) })} </View> );};//设置TabBarconst 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}> <ActiveScreen navigation={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' });//创建自定义TabBarconst 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'); }} />
0 0
- React Navigation 自认比较好的navigator组件(三)
- React Navigation 自认比较好的navigator组件(一)
- React Navigation 自认比较好的navigator组件(二)
- (三)React Native中Navigation组件的快速入门
- React Navigation--Stack Navigator 详细的例子
- React Navigation--Stack Navigator 详细的例子
- React Native 的 Navigator 组件使用方式
- React Native 的 Navigator 组件使用方式
- React Native 的 Navigator 组件(导航功能)
- React Navigation 入门(三)
- 自认为比较好的一篇英语文章记录一下。
- React Navigation--Stack Navigator Simple Example
- React Navigation之Stack Navigator完全解析
- React Navigation之Tab Navigator完全解析
- React Navigation--Stack Navigator and Tab Navigator联合使用
- React-Native中导航组件react-navigation的使用
- React Native系列——Navigator组件的使用介绍
- React Native]React Native组件之Navigator
- 从Redis中的BGSAVE命令谈起Fork—之二
- Github最火开源项目-RxjavaUtil
- MyEclipse+Tomcat+MAVEN环境搭建
- React Navigation 自认比较好的navigator组件(二)
- 前端面试ajax考点汇总
- React Navigation 自认比较好的navigator组件(三)
- 在MyEclispe中创建满足Maven的web项目
- js对象的浅拷贝和深拷贝
- 多进程编程的优缺点?
- 面试题25:二叉树中和为某一值的路径
- 使用log4j2实现日志数据脱敏
- 归并排序之Java实现
- 正态分布
- 阿里新开源Android库V