React Native中導(dǎo)航組件react-navigation跨tab路由處理詳解
前言
大家應(yīng)該都有所體會,我們在一般應(yīng)用都有跨tab跳轉(zhuǎn)的需求, 這就需要特別處理下路由,所以 下面是使用react-navigation作為路由組件的一種方式.
具體情境是: app分三大模塊Home主頁, Bill賬單和Me我的, 對應(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: [ // 棧里的路由信息會從 Home->HomeTwo 變成了 Bill->BillTwo
NavigationActions.navigate({ routeName: 'Bill'}),
NavigationActions.navigate({ routeName: 'BillTwo'})
]
});
從HomeTwo push 到 BillTwo頁面后, 點(diǎn)擊BillTwo的左上角導(dǎo)航按鈕返回就能返回到Bill賬單首頁了.
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- react-router-dom?v6?通過outlet實(shí)現(xiàn)keepAlive?功能的實(shí)現(xiàn)
- 詳解react-router-dom v6版本基本使用介紹
- react-router-domV6版本的路由和嵌套路由寫法詳解
- react-router6.x路由配置及導(dǎo)航詳解
- React路由規(guī)則定義與聲明式導(dǎo)航及編程式導(dǎo)航分別介紹
- React路由渲染方式與withRouter高階組件及自定義導(dǎo)航組件應(yīng)用詳細(xì)介紹
- 使用 React Router Dom 實(shí)現(xiàn)路由導(dǎo)航的詳細(xì)過程
相關(guān)文章
React使用TailwindCSS的實(shí)現(xiàn)示例
TailwindCSS是一個(gè)實(shí)用優(yōu)先的CSS框架,本文主要介紹了React使用TailwindCSS的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
react中的forwardRef 和memo的區(qū)別解析
forwardRef和memo是React中用于性能優(yōu)化和組件復(fù)用的兩個(gè)高階函數(shù),本文給大家介紹react中的forwardRef 和memo的區(qū)別及適用場景,感興趣的朋友跟隨小編一起看看吧2023-10-10
使用webpack5從0到1搭建一個(gè)react項(xiàng)目的實(shí)現(xiàn)步驟
這篇文章主要介紹了使用webpack5從0到1搭建一個(gè)react項(xiàng)目的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
react 實(shí)現(xiàn)頁面代碼分割、按需加載的方法
本篇文章主要介紹了react 實(shí)現(xiàn)頁面代碼分割、按需加載的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
關(guān)于antd tree和父子組件之間的傳值問題(react 總結(jié))
這篇文章主要介紹了關(guān)于antd tree 和父子組件之間的傳值問題,是小編給大家總結(jié)的一些react知識點(diǎn),本文通過一個(gè)項(xiàng)目需求實(shí)例代碼詳解給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-06-06
React?Hooks--useEffect代替常用生命周期函數(shù)方式
這篇文章主要介紹了React?Hooks--useEffect代替常用生命周期函數(shù)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
react中useState使用:如何實(shí)現(xiàn)在當(dāng)前表格直接更改數(shù)據(jù)
這篇文章主要介紹了react中useState的使用:如何實(shí)現(xiàn)在當(dāng)前表格直接更改數(shù)據(jù),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08

