react-navigation跨tab路由处理



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

具体情境是: app分三大模块Home主页, Bill账单和Me我的, 对应三个tab. 现在需求是 Home push HomeTwoHomeTwopush BillTwoBillTwo 返回到 Bill账单首页.

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

const Components = {
    HomeTwo: { screen: HomeTwo, path:'app/HomeTwo' },
    HomeThree: { screen: HomeThree, path:'app/HomeThree' },
    BillTwo: { screen: BillTwo, path:'app/BillTwo' },
    BillThree: { screen: BillThree, path:'app/BillThree' },
}

const Tabs = TabNavigator({
     Home: {
         screen: Home,
         path:'app/home',
         navigationOptions: { 
             tabBar: {
                 label: '首页',
                 icon: ({tintColor}) => (<Image source={require('./images/home.png')} style={[{tintColor: tintColor},styles.icon]}/>),
             },
         }
     },
     Bill: {
         screen: Bill,
         path:'app/bill',
         navigationOptions: {
             tabBar: {
                 label: '账单',
                 icon: ({tintColor}) => (<Image source={require('./images/bill.png')} style={[{tintColor: tintColor},styles.icon]}/>),
             },
         }
     },
     Me: {
         screen: Me,
         path:'app/me',
         navigationOptions: {
             tabBar: {
                 label: '我',
                 icon: ({tintColor}) => (<Image source={require('./images/me.png')} style={[{tintColor: tintColor},styles.icon]}/>),
             },
         }
     }
   }, {
       tabBarPosition: 'bottom', 
       swipeEnabled: false,
       animationEnabled: false, 
       lazyLoad: false, 
       backBehavior: 'none', 
       tabBarOptions: {
           activeTintColor: '#ff8500', 
           inactiveTintColor: '#999',
           showIcon: true, 
           indicatorStyle: {
               height: 0  
           },
           style: {
               backgroundColor: '#fff', 
           },
           labelStyle: {
               fontSize: 10, 
           },
       },
 });


 const Navs = StackNavigator({
     Home: { screen: Tabs, path:'app/Home' },
     Bill: { screen: Tabs, path:'app/Bill' },
     Me: { screen: Tabs, path:'app/Me' },
     ...Components
 }, {
        initialRouteName: 'Home', 
        navigationOptions: { 
            header: {  
                style: {
                    backgroundColor: '#fff'
                },
                titleStyle: {
                    color: 'green'
                }
            },
            cardStack: { 
                gesturesEnabled: true
            }
        },
        mode: 'card',  
        headerMode: 'screen'
 });

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

// 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'})
    ]
});

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


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

具体情境是: app分三大模块Home主页, Bill账单和Me我的, 对应三个tab. 现在需求是 Home push HomeTwoHomeTwopush BillTwoBillTwo 返回到 Bill账单首页.

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

const Components = {
    HomeTwo: { screen: HomeTwo, path:'app/HomeTwo' },
    HomeThree: { screen: HomeThree, path:'app/HomeThree' },
    BillTwo: { screen: BillTwo, path:'app/BillTwo' },
    BillThree: { screen: BillThree, path:'app/BillThree' },
}

const Tabs = TabNavigator({
     Home: {
         screen: Home,
         path:'app/home',
         navigationOptions: { 
             tabBar: {
                 label: '首页',
                 icon: ({tintColor}) => (<Image source={require('./images/home.png')} style={[{tintColor: tintColor},styles.icon]}/>),
             },
         }
     },
     Bill: {
         screen: Bill,
         path:'app/bill',
         navigationOptions: {
             tabBar: {
                 label: '账单',
                 icon: ({tintColor}) => (<Image source={require('./images/bill.png')} style={[{tintColor: tintColor},styles.icon]}/>),
             },
         }
     },
     Me: {
         screen: Me,
         path:'app/me',
         navigationOptions: {
             tabBar: {
                 label: '我',
                 icon: ({tintColor}) => (<Image source={require('./images/me.png')} style={[{tintColor: tintColor},styles.icon]}/>),
             },
         }
     }
   }, {
       tabBarPosition: 'bottom', 
       swipeEnabled: false,
       animationEnabled: false, 
       lazyLoad: false, 
       backBehavior: 'none', 
       tabBarOptions: {
           activeTintColor: '#ff8500', 
           inactiveTintColor: '#999',
           showIcon: true, 
           indicatorStyle: {
               height: 0  
           },
           style: {
               backgroundColor: '#fff', 
           },
           labelStyle: {
               fontSize: 10, 
           },
       },
 });


 const Navs = StackNavigator({
     Home: { screen: Tabs, path:'app/Home' },
     Bill: { screen: Tabs, path:'app/Bill' },
     Me: { screen: Tabs, path:'app/Me' },
     ...Components
 }, {
        initialRouteName: 'Home', 
        navigationOptions: { 
            header: {  
                style: {
                    backgroundColor: '#fff'
                },
                titleStyle: {
                    color: 'green'
                }
            },
            cardStack: { 
                gesturesEnabled: true
            }
        },
        mode: 'card',  
        headerMode: 'screen'
 });

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

// 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'})
    ]
});

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

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

具体情境是: app分三大模块Home主页, Bill账单和Me我的, 对应三个tab. 现在需求是 Home push HomeTwoHomeTwopush BillTwoBillTwo 返回到 Bill账单首页.

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

const Components = {
    HomeTwo: { screen: HomeTwo, path:'app/HomeTwo' },
    HomeThree: { screen: HomeThree, path:'app/HomeThree' },
    BillTwo: { screen: BillTwo, path:'app/BillTwo' },
    BillThree: { screen: BillThree, path:'app/BillThree' },
}

const Tabs = TabNavigator({
     Home: {
         screen: Home,
         path:'app/home',
         navigationOptions: { 
             tabBar: {
                 label: '首页',
                 icon: ({tintColor}) => (<Image source={require('./images/home.png')} style={[{tintColor: tintColor},styles.icon]}/>),
             },
         }
     },
     Bill: {
         screen: Bill,
         path:'app/bill',
         navigationOptions: {
             tabBar: {
                 label: '账单',
                 icon: ({tintColor}) => (<Image source={require('./images/bill.png')} style={[{tintColor: tintColor},styles.icon]}/>),
             },
         }
     },
     Me: {
         screen: Me,
         path:'app/me',
         navigationOptions: {
             tabBar: {
                 label: '我',
                 icon: ({tintColor}) => (<Image source={require('./images/me.png')} style={[{tintColor: tintColor},styles.icon]}/>),
             },
         }
     }
   }, {
       tabBarPosition: 'bottom', 
       swipeEnabled: false,
       animationEnabled: false, 
       lazyLoad: false, 
       backBehavior: 'none', 
       tabBarOptions: {
           activeTintColor: '#ff8500', 
           inactiveTintColor: '#999',
           showIcon: true, 
           indicatorStyle: {
               height: 0  
           },
           style: {
               backgroundColor: '#fff', 
           },
           labelStyle: {
               fontSize: 10, 
           },
       },
 });


 const Navs = StackNavigator({
     Home: { screen: Tabs, path:'app/Home' },
     Bill: { screen: Tabs, path:'app/Bill' },
     Me: { screen: Tabs, path:'app/Me' },
     ...Components
 }, {
        initialRouteName: 'Home', 
        navigationOptions: { 
            header: {  
                style: {
                    backgroundColor: '#fff'
                },
                titleStyle: {
                    color: 'green'
                }
            },
            cardStack: { 
                gesturesEnabled: true
            }
        },
        mode: 'card',  
        headerMode: 'screen'
 });

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

// 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'})
    ]
});

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


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

具体情境是: app分三大模块Home主页, Bill账单和Me我的, 对应三个tab. 现在需求是 Home push HomeTwoHomeTwopush BillTwoBillTwo 返回到 Bill账单首页.

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

const Components = {
    HomeTwo: { screen: HomeTwo, path:'app/HomeTwo' },
    HomeThree: { screen: HomeThree, path:'app/HomeThree' },
    BillTwo: { screen: BillTwo, path:'app/BillTwo' },
    BillThree: { screen: BillThree, path:'app/BillThree' },
}

const Tabs = TabNavigator({
     Home: {
         screen: Home,
         path:'app/home',
         navigationOptions: { 
             tabBar: {
                 label: '首页',
                 icon: ({tintColor}) => (<Image source={require('./images/home.png')} style={[{tintColor: tintColor},styles.icon]}/>),
             },
         }
     },
     Bill: {
         screen: Bill,
         path:'app/bill',
         navigationOptions: {
             tabBar: {
                 label: '账单',
                 icon: ({tintColor}) => (<Image source={require('./images/bill.png')} style={[{tintColor: tintColor},styles.icon]}/>),
             },
         }
     },
     Me: {
         screen: Me,
         path:'app/me',
         navigationOptions: {
             tabBar: {
                 label: '我',
                 icon: ({tintColor}) => (<Image source={require('./images/me.png')} style={[{tintColor: tintColor},styles.icon]}/>),
             },
         }
     }
   }, {
       tabBarPosition: 'bottom', 
       swipeEnabled: false,
       animationEnabled: false, 
       lazyLoad: false, 
       backBehavior: 'none', 
       tabBarOptions: {
           activeTintColor: '#ff8500', 
           inactiveTintColor: '#999',
           showIcon: true, 
           indicatorStyle: {
               height: 0  
           },
           style: {
               backgroundColor: '#fff', 
           },
           labelStyle: {
               fontSize: 10, 
           },
       },
 });


 const Navs = StackNavigator({
     Home: { screen: Tabs, path:'app/Home' },
     Bill: { screen: Tabs, path:'app/Bill' },
     Me: { screen: Tabs, path:'app/Me' },
     ...Components
 }, {
        initialRouteName: 'Home', 
        navigationOptions: { 
            header: {  
                style: {
                    backgroundColor: '#fff'
                },
                titleStyle: {
                    color: 'green'
                }
            },
            cardStack: { 
                gesturesEnabled: true
            }
        },
        mode: 'card',  
        headerMode: 'screen'
 });

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

// 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'})
    ]
});

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值