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