強烈推薦使用該三方
如果在使用過程中遇到什么問題,可以加入react-native興趣交流群
群號:397885169
一起討論學(xué)習(xí),也歡迎在評論區(qū)評論。
RN項目模板,還未完成,但react-native-router-flux的Demo在里面
本文會持續(xù)更新,只要該庫作者不停止更新,那么我也不會停下腳步。
可能很多人之前就用過這個三方,也可能有些人聽過沒用過。在這系列文章中,有這樣幾方面原因:
通用性:在最新的V4版本中是基于
react-navigation
實現(xiàn)的,如果使用過react-native-router-flux
那么使用新版本的學(xué)習(xí)成本會低很多;實用性:
react-navigation
雖然是官方推薦的導(dǎo)航庫,但其庫內(nèi)部提供的,可以直接使用的功能很簡單,有些還需要配合redux來實現(xiàn)需要的功能。而react-native-router-flux
基于react-navigation
實現(xiàn)其沒有提供的APIpopTo(跳回指定頁面)
,refresh(刷新頁面)
,replace
,Modal(類似iOS從底部彈出個新頁面的效果)
等常用到的功能,在下面的翻譯中有詳細(xì)說明;更新維護(hù):這點上我很佩服庫的作者,從V1更新到V4,從未背離作者的初衷,一直在對
react-native
的導(dǎo)航進(jìn)行優(yōu)化、封裝,而且最讓我佩服的一點是,作者好似將react-navigation
的Issues
全都翻看過,庫里面將react-navigation
可能存在或者已經(jīng)存在的坑都填上了,而且實時更新。如果有時間,可以查看一下CHANGELOG.md,里面有著全部的更新記錄。
稍后我會提供一個相對完整的Demo來作為對本文的補充,當(dāng)然,其實作者提供的Demo已經(jīng)很棒了,但作者將很多功能放在了一起,第一次接觸的時候,很可能會被繞進(jìn)去。
Available imports
Router
Scene
Tabs
Stack
Tabbed Scene
Drawer
Modal
Lightbox
Actions
ActionConst
Router:
Property | Type | Default | Description |
---|---|---|---|
children | required | 頁面根組件 | |
wrapBy |
Function |
允許集成諸如Redux(connect)和Mobx(observer)之類的狀態(tài)管理方案 | |
sceneStyle |
Style |
適用于所有場景的Style(可選) | |
backAndroidHandler |
Function |
允許在Android中自定義控制返回按鈕(可選)。有關(guān)更多信息,請查看BackHandler | |
uriPrefix |
string |
通過uri來深層鏈接,從App外跳入App內(nèi)的某個頁面,如果您想支持www.example.com/user/1234/ 的深度鏈接,可以通過example.com 將路徑匹配到/user/1234/
|
backAndroidHandler用法:
const onBackPress = () => {
if (Actions.state.index !== 0) {
return false
}
Actions.pop()
return true
}
backAndroidHandler={onBackPress}
Scene:
此路由器的最重要的組件, 所有 <Scene>
組件必須要有一個唯一的 key
。父節(jié)點<Scene>
不能將component
作為prop
,因為它將作為其子節(jié)點的組件。
Property | Type | Default | Description |
---|---|---|---|
key |
string |
required |
將用于標(biāo)識頁面,例如Actions.name(params) 。必須是獨一無二的 |
path |
string |
將被用來匹配傳入的深層鏈接和傳遞參數(shù),例如:/user/:id/ 將從/user/1234/ 用params {id:1234}調(diào)用場景的操作。接受uri的模板標(biāo)準(zhǔn)https://tools.ietf.org/html/rfc6570
|
|
component |
React.Component |
semi-required |
要顯示的組件,定義嵌套時不需要Scene ,參見示例。 |
back |
boolean |
false |
如果是true ,則顯示后退按鈕,而不是由上層容器定義的左側(cè)/drawer按鈕。 |
backButtonImage |
string |
設(shè)置返回按鈕的圖片 | |
backButtonTintColor |
string |
自定義后退按鈕色調(diào) | |
init |
boolean |
false |
如果是true 后退按鈕不會顯示 |
clone |
boolean |
false |
標(biāo)有clone 的場景將被視為模板,并在被推送時克隆到當(dāng)前場景的父節(jié)點中。詳情請參見示例。 |
contentComponent |
React.Component |
用于呈現(xiàn)抽屜內(nèi)容的組件(例如導(dǎo)航)。 | |
drawer |
boolean |
false |
載入DrawerNavigator內(nèi)的子頁面 |
failure |
Function |
如果on 返回一個“falsey”值,那么failure 將被調(diào)用。 |
|
backTitle |
string |
指定場景的后退按鈕標(biāo)題 | |
backButtonTextStyle |
Style |
用于返回按鈕文本的樣式 | |
rightTitle |
string |
為場景指定右側(cè)的按鈕標(biāo)題 | |
headerMode |
string |
float |
指定標(biāo)題應(yīng)該如何呈現(xiàn):(float渲染單個標(biāo)題,保持在頂部,動畫隨著屏幕的變化,這是iOS上的常見樣式。)screen(每個屏幕都有一個標(biāo)題,并且標(biāo)題淡入,與屏幕一起出現(xiàn),這是Android上的常見模式)如果為none(不會顯示標(biāo)題) |
hideNavBar |
boolean |
false |
隱藏導(dǎo)航欄 |
hideTabBar |
boolean |
false |
隱藏標(biāo)簽欄(僅適用于擁有tabs 指定的場景) |
hideBackImage |
boolean |
false |
隱藏返回圖片 |
initial |
boolean |
false |
設(shè)置為true 后,會默認(rèn)顯示該頁面 |
leftButtonImage |
Image |
替換左側(cè)按鈕圖片 | |
leftButtonTextStyle |
Style |
左側(cè)按鈕的文字樣式 | |
leftButtonStyle |
Style |
左側(cè)按鈕的樣式 | |
leftButtonIconStyle |
Style |
左側(cè)按鈕的圖標(biāo)樣式 | |
modal |
boolean |
false |
將場景容器定義為modal ,即所有子場景都將從底部彈起到頂部。它僅適用于containers(與v3版本的語法不同) |
navBar |
React.Component |
可以使用自定義的React組件來定義導(dǎo)航欄 | |
navBarButtonColor |
string |
設(shè)置導(dǎo)航欄返回按鈕的顏色 | |
navigationBarStyle |
Style |
導(dǎo)航欄的樣式 | |
navigationBarTitleImage |
Object |
導(dǎo)航欄中的圖像中覆蓋title 的Image
|
|
navigationBarTitleImageStyle |
object |
navigationBarTitleImage 的樣式 |
|
navTransparent |
boolean |
false |
導(dǎo)航欄是否透明 |
on |
Function |
又名 onEnter
|
|
onEnter |
Function |
當(dāng)Scene 要被跳轉(zhuǎn)時調(diào)用。props 將被作為參數(shù)提供。只支持定義了'component'的場景。 |
|
onExit |
Function |
當(dāng)Scene 要跳轉(zhuǎn)離開時調(diào)用。只支持定義了'component'的場景。 |
|
onLeft |
Function |
當(dāng)導(dǎo)航欄左側(cè)按鈕被點擊時調(diào)用。 | |
onRight |
Function |
當(dāng)導(dǎo)航欄右側(cè)按鈕被點擊時調(diào)用。 | |
renderTitle |
React.Component |
使用React組件顯示導(dǎo)航欄的title | |
renderLeftButton |
React.Component |
使用React組件顯示導(dǎo)航欄的左側(cè)按鈕 | |
renderRightButton |
React.Component |
使用React組件顯示導(dǎo)航欄的右側(cè)按鈕 | |
renderBackButton |
React.Component |
使用React組件顯示導(dǎo)航欄的返回按鈕 | |
rightButtonImage |
Image |
設(shè)置右側(cè)按鈕的圖片 | |
rightButtonTextStyle |
Style |
右側(cè)按鈕文字的樣式 | |
success |
Function |
如果on 返回一個"真實"的值,那么success 將被調(diào)用。 |
|
tabs |
boolean |
false |
將子場景加載為TabNavigator。其他標(biāo)簽導(dǎo)航器屬性也是適用的。 |
title |
string |
要顯示在導(dǎo)航欄中心的文本。 | |
titleStyle |
Style |
title的樣式 | |
type |
string |
push |
可選的導(dǎo)航操作。你可以使用replace 來替換此場景中的當(dāng)前場景 |
all other props | 此處未列出的其他屬性將轉(zhuǎn)交給Scene 的component
|
Tabs (<Tabs>
or <Scene tabs>
)
標(biāo)簽欄組件。
你可以使用<Scene>
中的所有props
來作為<Tabs>
的屬性。 如果要使用該組件需要設(shè)置 <Scene tabs={true}>
。
Property | Type | Default | Description |
---|---|---|---|
wrap |
boolean |
true |
自動使用自己的導(dǎo)航欄包裝每個場景(如果不是另一個容器)。 |
activeBackgroundColor |
string |
指定焦點的選項卡的選中背景顏色 | |
activeTintColor |
string |
指定標(biāo)簽欄圖標(biāo)的選中色調(diào)顏色 | |
inactiveBackgroundColor |
string |
指定非焦點的選項卡的未選中背景顏色 | |
inactiveTintColor |
string |
指定標(biāo)簽欄圖標(biāo)的未選中色調(diào)顏色 | |
labelStyle |
object |
設(shè)置tabbar上文字的樣式 | |
lazy |
boolean |
false |
在選項卡處于活動狀態(tài)之前,不會渲染選項卡場景(推薦設(shè)置成true) |
tabBarComponent |
React.Component |
使用React組件以自定義標(biāo)簽欄 | |
tabBarPosition |
string |
指定標(biāo)簽欄位置。iOS上默認(rèn)為bottom ,安卓上是top 。 |
|
tabBarStyle |
object |
標(biāo)簽欄演示 | |
tabStyle |
object |
單個選項卡的樣式 | |
showLabel |
boolean |
true |
是否顯示標(biāo)簽欄文字 |
swipeEnabled |
boolean |
true |
是否可以滑動選項卡。 |
animationEnabled |
boolean |
true |
切換動畫 |
tabBarOnPress |
function |
自定義tabbar點擊事件 | |
backToInitial |
boolean |
false |
如果選項卡圖標(biāo)被點擊,返回到默認(rèn)選項卡。 |
Stack (<Stack>
)
將場景組合在一起的組件,用于自己的基于堆棧實現(xiàn)的導(dǎo)航。使用它將為此堆棧創(chuàng)建一個單獨的navigator,因此,除非您添加hideNavBar
,否則將會出現(xiàn)兩個導(dǎo)航條。
Tab Scene (child <Scene>
within Tabs
)
用于實現(xiàn)Tabs
的效果展示,可以自定義icon和label。
Property | Type | Default | Description |
---|---|---|---|
icon |
component |
undefined |
作為選項卡圖標(biāo)放置的RN組件 |
tabBarLabel |
string |
tabbar上的文字 |
Drawer (<Drawer>
or <Scene drawer>
)
用于實現(xiàn)抽屜的效果,如果要使用該組件需要設(shè)置 <drawer tabs={true}>
。
Property | Type | Default | Description |
---|---|---|---|
drawerImage |
Image |
替換抽屜'hamburger'圖標(biāo),你必須把它與drawer 一起設(shè)置 |
|
drawerIcon |
React.Component |
用于抽屜'hamburger'圖標(biāo)的任意組件,您必須將其與drawer 道具一起設(shè)置 |
|
hideDrawerButton |
boolean |
false |
是否顯示drawerImage 或者drawerIcon
|
drawerPosition |
string |
抽屜是在右邊還是左邊??蛇x屬性right 或left
|
|
drawerWidth |
number |
抽屜的寬度(以像素為單位)(可選) | |
drawerLockMode |
enum('unlocked', 'locked-closed', 'locked-open') |
指定抽屜的鎖模式(https://facebook.github.io/react-native/docs/drawerlayoutandroid.html#drawerlockmode) |
Modals (<Modal>
or <Scene modal>
)
想要實現(xiàn)模態(tài),您必須將其<Modal>
作為您Router
的根場景。在Modal
將正常呈現(xiàn)第一個場景(應(yīng)該是你真正的根場景),它將渲染第一個元素作為正常場景,其他所有元素作為彈出窗口(當(dāng)它們 被push)。
示例:在下面的示例中,root
場景嵌套在<Modal>
中,因為它是第一個嵌套Scene
,所以它將正常呈現(xiàn)。如果要push
到statusModal
,errorModal
或者loginModal
,他們將呈現(xiàn)為Modal
,默認(rèn)情況下會從屏幕底部向上彈出。重要的是要注意,目前Modal
不允許透明的背景。
//... import components
<Router>
<Modal>
<Scene key="root">
<Scene key="screen1" initial={true} component={Screen1} />
<Scene key="screen2" component={Screen2} />
</Scene>
<Scene key="statusModal" component={StatusModal} />
<Scene key="errorModal" component={ErrorModal} />
<Scene key="loginModal" component={LoginModal} />
</Modal>
</Router>
Lightbox (<Lightbox>
)
Lightbox
是用于將組件渲染在當(dāng)前組件上Scene
的組件 。與Modal
不同,它將允許調(diào)整大小和背景的透明度。
示例:
在下面的示例中,root
場景嵌套在中<Lightbox>,因為它是第一個嵌套Scene
,所以它將正常呈現(xiàn)。如果要push
到loginLightbox
,他們將呈現(xiàn)為Lightbox
,默認(rèn)情況下將放置在當(dāng)前場景的頂部,允許透明的背景。
//... import components
<Router>
<Lightbox>
<Scene key="root">
<Scene key="screen1" initial={true} component={Screen1} />
<Scene key="screen2" component={Screen2} />
</Scene>
{/* Lightbox components will lay over the screen, allowing transparency*/}
<Scene key="loginLightbox" component={loginLightbox} />
</Lightbox>
</Router>
Actions
該對象的主要工具是為您的應(yīng)用程序提供導(dǎo)航功能。 假設(shè)您的Router
和Scenes
配置正確,請使用下列屬性在場景之間導(dǎo)航。 有些提供添加的功能,將React道具傳遞到導(dǎo)航場景。
這些可以直接使用,例如,Actions.pop()
將在源代碼中實現(xiàn)的操作,或者,您可以在場景類型中設(shè)置這些常量,當(dāng)您執(zhí)行Actions.main()
時,它將根據(jù)您的場景類型或默認(rèn)值來執(zhí)行動作。
Property | Type | Parameters | Description |
---|---|---|---|
[key] |
Function |
Object |
Actions 將'自動'使用路由器中的場景key 進(jìn)行導(dǎo)航。如果需要跳轉(zhuǎn)頁面,可以直接使用Actions.key() 或Actions[key].call()
|
currentScene |
String |
返回當(dāng)前活動的場景 | |
jump |
Function |
(sceneKey: String, props: Object) |
用于切換到新選項卡. For Tabs only. |
pop |
Function |
回到上一個頁面 | |
popTo |
Function |
(sceneKey: String, props: Object) |
返回到指定的頁面 |
push |
Function |
(sceneKey: String, props: Object) |
跳轉(zhuǎn)到新頁面 |
refresh |
Function |
(props: Object) |
重新加載當(dāng)前頁面 |
replace |
Function |
(sceneKey: String, props: Object) |
從堆棧中彈出當(dāng)前場景,并將新場景推送到導(dǎo)航堆棧。沒有過度動畫。 |
reset |
Function |
(sceneKey: String, props: Object) |
清除路由堆棧并將場景推入第一個索引. 沒有過渡動畫。 |
drawerOpen |
Function |
如果可用,打開Drawer
|
|
drawerClose |
Function |
如果可用,關(guān)閉Drawer
|
ActionConst
鍵入常量以確定Scene轉(zhuǎn)換,這些是優(yōu)先于手動鍵入其值,因為項目更新時可能會發(fā)生更改。
Property | Type | Value | Shorthand |
---|---|---|---|
ActionConst.JUMP |
string |
'REACT_NATIVE_ROUTER_FLUX_JUMP' | jump |
ActionConst.PUSH |
string |
'REACT_NATIVE_ROUTER_FLUX_PUSH' | push |
ActionConst.PUSH_OR_POP |
string |
'REACT_NATIVE_ROUTER_FLUX_PUSH_OR_POP' | push |
ActionConst.REPLACE |
string |
'REACT_NATIVE_ROUTER_FLUX_REPLACE' | replace |
ActionConst.BACK |
string |
'REACT_NATIVE_ROUTER_FLUX_BACK' | pop |
ActionConst.BACK_ACTION |
string |
'REACT_NATIVE_ROUTER_FLUX_BACK_ACTION' | pop |
ActionConst.POP_TO |
string |
'REACT_NATIVE_ROUTER_FLUX_POP_TO' | popTo |
ActionConst.REFRESH |
string |
'REACT_NATIVE_ROUTER_FLUX_REFRESH' | refresh |
ActionConst.RESET |
string |
'REACT_NATIVE_ROUTER_FLUX_RESET' | reset |
ActionConst.FOCUS |
string |
'REACT_NATIVE_ROUTER_FLUX_FOCUS' | N/A |
ActionConst.BLUR |
string |
'REACT_NATIVE_ROUTER_FLUX_BLUR' | N/A |
ActionConst.ANDROID_BACK |
string |
'REACT_NATIVE_ROUTER_FLUX_ANDROID_BACK' | N/A |
Universal and Deep Linking
用例
- 考慮這樣一個web應(yīng)用程序和app配對,這可能有一個url
https://thesocialnetwork.com/profile/1234/.
- 如果我們同時構(gòu)建一個web應(yīng)用程序和一個移動應(yīng)用程序,我們希望能夠通過path
/profile/:id/
。 - 在web上,我們可能想要用一個路由器來打開我們的
<Profile />
和參數(shù){ id: 1234 }
- 在移動設(shè)備上,如果我們正確地設(shè)置了Android / iOS環(huán)境來啟動我們的應(yīng)用程序并打開RNRF
<Router />,
,那么我們還需要導(dǎo)航到我們的移動<Profile />
場景和參數(shù){ id: 1234 }
使用
<Router uriPrefix={'thesocialnetwork.com'}>
<Scene key="root">
<Scene key={'home'} component={Home} />
<Scene key={'profile'} path={"/profile/:id/"} component={Profile} />
<Scene key={'profileForm'} path={"/edit/profile/:id/"} component={ProfileForm} />
</Scene>
</Router>
如果用戶點擊http://thesocialnetwork.com/profile/1234/
在他們的設(shè)備,他們會打開<Router/ >
,然后調(diào)用操作Actions.profile({ id:1234 })