react-navigation跨tab路由处理

前端之家收集整理的这篇文章主要介绍了react-navigation跨tab路由处理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


一般应用都有跨tab跳转的需求,这就需要特别处理下路由. 下面是使用React-navigation作为路由组件的一种方式.http://www.bijishequ.com/detail/369321

具体情境是: app分三大模块Home主页,Bill账单和Me我的,对应三个tab. 现在需求是HomepushHomeTwo,244);">HomeTwopushBillTwo,244);">BillTwo返回到Bill账单首页.

首先选择路由结构,选择使用最外层是StackNavigator,然后包含3个TabNavigator和其他组件.

  1. const Components = {
  2. HomeTwo: { screen: HomeTwo,path:'app/HomeTwo' },HomeThree: { screen: HomeThree,path:'app/HomeThree' },BillTwo: { screen: BillTwo,path:'app/BillTwo' },BillThree: { screen: BillThree,path:'app/BillThree' },}
  3.  
  4. const Tabs = TabNavigator({
  5. Home: {
  6. screen: Home,path:'app/home',navigationOptions: {
  7. tabBar: {
  8. label: '首页',icon: ({tintColor}) => (<Image source={require('./images/home.png')} style={[{tintColor: tintColor},styles.icon]}/>),},}
  9. },Bill: {
  10. screen: Bill,path:'app/bill',navigationOptions: {
  11. tabBar: {
  12. label: '账单',icon: ({tintColor}) => (<Image source={require('./images/bill.png')} style={[{tintColor: tintColor},Me: {
  13. screen: Me,path:'app/me',navigationOptions: {
  14. tabBar: {
  15. label: '我',icon: ({tintColor}) => (<Image source={require('./images/me.png')} style={[{tintColor: tintColor},}
  16. }
  17. },{
  18. tabBarPosition: 'bottom',swipeEnabled: false,animationEnabled: false,lazyLoad: false,backBehavior: 'none',tabBarOptions: {
  19. activeTintColor: '#ff8500',inactiveTintColor: '#999',showIcon: true,indicatorStyle: {
  20. height: 0
  21. },style: {
  22. backgroundColor: '#fff',labelStyle: {
  23. fontSize: 10,});
  24.  
  25.  
  26. const Navs = StackNavigator({
  27. Home: { screen: Tabs,path:'app/Home' },Bill: { screen: Tabs,path:'app/Bill' },Me: { screen: Tabs,path:'app/Me' },...Components
  28. },{
  29. initialRouteName: 'Home',navigationOptions: {
  30. header: {
  31. style: {
  32. backgroundColor: '#fff'
  33. },titleStyle: {
  34. color: 'green'
  35. }
  36. },cardStack: {
  37. gesturesEnabled: true
  38. }
  39. },mode: 'card',headerMode: 'screen'
  40. });

HomeTwo里使用react-navigation自带reset action就可以重置路由信息了:

@H_502_42@// push BillTwo this.props.navigation.dispatch(resetAction); // 使用reset action重置路由 const resetAction = NavigationActions.reset({ index: 1,// 注意不要越界 actions: [ // 栈里的路由信息会从 Home->HomeTwo 变成了 Bill->BillTwo NavigationActions.navigate({ routeName: 'Bill'}),NavigationActions.navigate({ routeName: 'BillTwo'}) ] });

HomeTwopush 到BillTwo页面后,点击BillTwo的左上角导航按钮返回就能返回到Bill账单首页了.


http://www.bijishequ.com/detail/369321

react-navigation自带Bill账单首页了.

React-navigation作为路由组件的一种方式.http://www.bijishequ.com/detail/369321

Bill账单首页.

TabNavigator和其他组件.

  1. reset action就可以重置路由信息了:

  2. Bill账单首页了.

  3.   
  4.   http://www.bijishequ.com/detail/369321 
  5.   

    Bill账单首页了.

  6.  

猜你在找的React相关文章