react-navigation跨tab路由處理

一般應(yīng)用都有跨tab跳轉(zhuǎn)的需求, 這就需要特別處理下路由. 下面是使用react-navigation作為路由組件的一種方式.

具體情境是: app分三大模塊Home主頁, Bill賬單和Me我的, 對(duì)應(yīng)三個(gè)tab. 現(xiàn)在需求是 Home push HomeTwo, HomeTwo push BillTwo, BillTwo 返回到 Bill賬單首頁.

首先選擇路由結(jié)構(gòu), 選擇使用最外層是StackNavigator, 然后包含3個(gè)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: [  // 棧里的路由信息會(huì)從 Home->HomeTwo 變成了 Bill->BillTwo
        NavigationActions.navigate({ routeName: 'Bill'}),
        NavigationActions.navigate({ routeName: 'BillTwo'})
    ]
});

HomeTwo push 到 BillTwo頁面后, 點(diǎn)擊BillTwo的左上角導(dǎo)航按鈕返回就能返回到Bill賬單首頁了.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容