react-native-router-flux組件基礎(chǔ)使用教程分為一二兩部分教程。教程一主要講解router-flux的使用方式,教程二主router-flux官方提供的各種API。
react-native-router-flux 組件用法詳解(一)
Demo示例
https://github.com/guangqiang-liu/react-native-routerFluxDemo
官方常用API
Available imports
- Router
- Scene
- Tabs
- Tabbed Scene
- Drawer
- Modal
- Lightbox
- Actions
- ActionConst
Router
Property | Type | Default | Description |
---|---|---|---|
createReducer | Function | 該函數(shù)功能:createReducer({initialState, scenes})將返回一個(gè)reducer,你可以用你自定義的reducer綁定一個(gè)Reducer(param),可以參看下面章節(jié)中Flux的用例 | |
dispatch | Function | ||
state | object | ||
scenes | Function | Style applied to all scenes (optional) | |
navigator | Function | ||
wrapBy | Function | function to wrap each Scene component and nav bar buttons - allows easy MobX integration (by passing observer) | |
getSceneStyle | Function | 根據(jù)需要重寫該樣式去改變導(dǎo)航卡的動(dòng)畫。 | |
sceneStyle | object | ||
children | React.Component | required | Scene root element |
backAndroidHandler | Function | 可以重寫該方法去控制android設(shè)備的返回鍵,返回true時(shí)會(huì)停留在app內(nèi)部,返回false時(shí)會(huì)直接退出app,默認(rèn)的操作是從棧中移除棧頂?shù)膕cene,如果該scene是最后一個(gè),就會(huì)退出app |
Scene
Property | Type | Default | Description |
---|---|---|---|
key | string | required | 在切換屏幕的時(shí)候會(huì)使用該key,例如Actions.name(params)。key的值必須時(shí)唯一的。 |
component | nt React.Component | semi-required | 切換到該scene時(shí),component屬性定義的組件將被展示出來。當(dāng)定義一個(gè)嵌套的scene時(shí)不要求有。例如。如果他作為一個(gè)scene容器定義。他將被視作一個(gè)自定義容器渲染者來使用。 |
initial | bool | false | 如果設(shè)置該屬性為true,該scene將成為默認(rèn)初始化scene。你可以理解為進(jìn)來后進(jìn)一個(gè)看到的scene |
type | string | push | 定義如何去創(chuàng)建一個(gè)新的屏幕并放入導(dǎo)航棧中。可以是ActionConst.PUSH,AtionConst.JUMP,ActionConst.REPLACK,AtionConst.RESET.如果父容器是一個(gè)tabbar且tabs=true,將會(huì)自動(dòng)設(shè)置為ActionConst.JUMP |
clone | boolean | false | 在被push的時(shí)候,使用clone標(biāo)識(shí)的Scenes將被作為模版處理,并克隆到當(dāng)前的scene的容器中。 |
all other props |
Stack (<Stack>
)
A component to group Scenes together for its own stack based navigation. Using this will create a separate navigator for this stack, so expect two navbars to appear unless you add hideNavBar.
Tab Scene (child <Scene> within Tabs
)
Property | Type | Default | Description |
---|---|---|---|
icon | component | a React Native component to place as a tab icon | |
tabBarLabel | string | The string to override a tab label |
Drawer(<Drawer> or <Scene drawer>
)
Can use all prop as listed in Scene as <Drawer>, syntatic sugar for <Scene drawer={true}>
Property | Type | Default | Description |
---|---|---|---|
drawerImage | Image | Image to substitute drawer 'hamburger' icon, you have to set it together with drawer prop | |
drawerIcon | React.Component | Arbitrary component to be used for drawer 'hamburger' icon, you have to set it together with drawer prop | |
hideDrawerButton | boolean | false | Boolean to show or not the drawerImage or drawerIcon |
drawerPosition | string | left | Determines whether the drawer is on the right or the left. Keywords accepted are right and left |
drawerWidth | number | The width, in pixels, of the drawer (optional) |
Modals (<Modal> or <Scene modal>
)
To implement a modal, you must use <Modal> as the root scene in your Router. The Modal will render the first scene (should be your true root scene) normally, and all following To display a modal use <Modal> as root renderer, so it will render the first element as normal scene and all others as popups (when they are pushed).
Example: In the example below, the root Scene is nested within a <Modal>, since it is the first nested Scene, it will render normally. If one were to push to statusModal, errorModal or loginModal, they will render as a Modal and by default will pull up from the bottom of the screen. It is important to note that currently the Modal does not allow for transparent backgrounds.
//... 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 is a component used to render a component on top of the current Scene. Unlike modal, it will allow for resizing and transparency of the background.
Example: In the example below, the root Scene is nested within a <Lightbox>, since it is the first nested Scene, it will render normally. If one were to push to loginLightbox, they will render as a Lightbox and by default will lay on top of the current Scene allowing for transparent backrounds.
//... 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
This Object is the main utility is to provide navigation features to your application. Assuming your Router and Scenes are configured properly, use the properties listed below to navigate between scenes. Some offer the added functionality to pass React props to the navigated scene.
These can be used directly, for example, Actions.pop() will dispatch correspond action written in the source code, or, you can set those constants in scene type, when you do Actions.main(), it will dispatch action according to your scene type or the default one.
Property | Type | Default | Description |
---|---|---|---|
[key] | Function | Object | The Actions object "automagically" uses the Scene's key prop in the Router to navigate. To navigate to a scene, call Actions.key() or Actions[key].call(). |
currentScene | String | Returns the current scene that is active | |
jump | Function | (sceneKey: String, props: Object) | used to switch to a new tab. For Tabs only. |
pop | Function | Go back to the previous scene by "popping" the current scene off the nav stack | |
popTo | Function | (sceneKey: String, props: Object) | Pops the navigation stack until the Scene with the specified key is reached. |
push | Function | (sceneKey: String, props: Object) | Pushes the scene to the stack, performing a transition to the new scene. |
refresh | Function | (props: Object) | Reloads the current scene by loading new props into the Scene |
replace | Function | (sceneKey: String, props: Object) | Pops the current scene from the stack and pushes the new scene to the navigation stack. *No transition will occur. |
reset | Function | (sceneKey: String, props: Object) | Clears the routing stack and pushes the scene into the first index. No transition will occur. |
drawerOpen | Function | Opens the Drawer if applicable | |
drawerClose | Function | Closes the Drawer if applicable |
ActionConst
在定義scene類型或者action參數(shù)時(shí),我們可以指定類型
Actions.ROUTE_NAME({type: 'reset'})
<Scene key="myscene" type="replace" >
但是當(dāng)傳入reducer時(shí),它將被轉(zhuǎn)換成一個(gè)靜態(tài)常量值,為了一致性,我們推薦總是使用常量的去替換字符串
Actions.ROUTE_NAME({type: ActionConst.RESET})
<Scene key="myscene" type={ActionConst.REPLACE} >
Type constants to determine Scene transitions, These are PREFERRED over typing their values manually as these are subject to change as the project is updated.
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 |
- replace:告訴導(dǎo)航器使用一個(gè)新的route來替換當(dāng)前的route
- actionSheet:以彈出的方式展示一個(gè)Action Sheet,你必須傳入一個(gè)回調(diào)作為回調(diào)方法。
- modal:在導(dǎo)航組件后的路由棧中插入該類型定義的組件。它可以被作為一個(gè)彈出的提示框使用,也可以在任何導(dǎo)航傳輸之前(像登錄授權(quán)處理)做一些必須處理的操作進(jìn)行使用。我們可以使用Actions.dismiss()關(guān)閉它。(類似android原生種的* dialog,ios中的模態(tài)視圖)。
- switch:在tab 場景下使用。(一般是點(diǎn)擊底部按鈕切換不同的scene)。
- reset:類似replace,但是它在導(dǎo)航棧中卸載了該組件。
- transitionToTop:如果路由有sceneConfig配置,如: ,將根據(jù)name重置路由堆棧中的路由和動(dòng)畫。
Animation
Property | Type | Default | Description |
---|---|---|---|
duration | number | 可選的。 充當(dāng)在給定持續(xù)時(shí)間(以ms為單位)中使用Animated.timing編寫applyAnimation函數(shù)的快捷方式。 | |
direction | string | horizontal | 動(dòng)畫的方向 水平/垂直/左到右 (水平即從右到左) |
animation | string | 在轉(zhuǎn)換時(shí)的動(dòng)畫選項(xiàng),當(dāng)前只有fade(漸變) | |
animationStyle | function | 用于場景轉(zhuǎn)換的可選內(nèi)插函數(shù):animationStyle = {interpolationFunction} | |
applyAnimation | function | 可選,如果提供將覆蓋默認(rèn)的彈簧動(dòng)畫 |
Gestures(手勢)
Property | Type | Default | Description |
---|---|---|---|
panHandlers | object | 可選的,置為null可以關(guān)閉滑動(dòng)返回手勢。 | |
getPanHandlers | function | 可選的去重寫一個(gè)scene的手勢操作 |
Scene styles
Property | Type | Default | Description |
---|---|---|---|
sceneStyle | style | { flex: 1 } | 場景組件的可選樣式覆蓋 |
getSceneStyle | function | 可以覆蓋NavigationCard的Animated.View渲染場景的樣式。 接收NavigationSceneRendererProps的第一個(gè)參數(shù)和{hideNavBar,hideTabBar,isActive}的第二個(gè)參數(shù)。 |
Tabs (<Tabs> or <Scene tabs>
)
Property | Type | Default | Description |
---|---|---|---|
tabs | bool | false | 定義TabBar場景容器以便子場景可以作為tabs展示。如果沒有組件被定義,內(nèi)置的TabBar 將作為容器。所有的子場景都被包裹到自己的導(dǎo)航條中。 |
tabBarStyle | style | 可以選擇重寫去定義Tabs組件的樣式 | |
tabBarBackgroundImage | string | 可以選擇重寫去定義Tabs組件的背景圖片 | |
tabBarIconContainerStyle | style | 可以選擇重寫去定義包含每個(gè)tab icon的vie容器的樣式 | |
hideTabBar | bool | false | 隱藏此場景的選項(xiàng)卡欄和任何后續(xù)場景,直到顯式反轉(zhuǎn)(如果內(nèi)置TabBar組件用作父渲染器) |
hideOnChildTabs | bool | false | 當(dāng)另一個(gè)選項(xiàng)卡場景添加到導(dǎo)航堆棧時(shí),隱藏被添加到當(dāng)行欄的場景的選項(xiàng)卡欄。 |
pressOpacity | number | 0.2 | 點(diǎn)擊選項(xiàng)卡時(shí)的透明度,默認(rèn)0.2 |
Navigation Bar
Property | Type | Default | Description |
---|---|---|---|
hideNavBar | bool | false | 隱藏當(dāng)前scene的導(dǎo)航欄 |
navigationBarStyle | style | 可以重寫該屬性去定義導(dǎo)航欄 | |
navigationBarBackgroundImage | string | 重寫該屬性去設(shè)置導(dǎo)航欄的背景圖片 | |
navBar | React.Component | 通過該屬性設(shè)置自定義的導(dǎo)航欄。可以參考內(nèi)置的導(dǎo)航欄組件 | |
drawerImage | string | require('./menu_burger.png') |
Navigation Bar(Title)
Property | Type | Default | Description |
---|---|---|---|
title | string | 設(shè)置導(dǎo)航欄標(biāo)題 | |
getTitle | function | 根據(jù)state返回標(biāo)題 | |
renderTitle | function | Optionally closure to render the title | |
titleStyle | Text style | 重寫標(biāo)題的樣式 | |
titleOpacity | string | 在導(dǎo)航欄中設(shè)置不透明的標(biāo)題 | |
titleProps | object | 任何其他的屬性都會(huì)被設(shè)置到標(biāo)題組件上 |
Navigation Bar(Back button)
Property | Type | Default | Description |
---|---|---|---|
backTitle | string | optional string to display with back button | |
renderBackButton | function | 如果該路由恰好是之前的路由,關(guān)閉重新渲染返回按鈕文字或者按鈕的行為 | |
backButtonImage | string | optional style override for the back title element | |
hideBackImage | boolean | false | 隱藏返回按鈕圖片 |
onBack | function | Actions.pop | 點(diǎn)擊返回按鈕時(shí)的行為,默認(rèn)是Actions.pop |
Navigation Bar(Left button)
Property | Type | Default | Description |
---|---|---|---|
leftTitle | string | optional string to display on the left if the previous route does not provide renderBackButton prop. renderBackButton > leftTitle> | |
getLeftTitle | function | optional closure to display on the left if the previous route does not provide renderBackButton prop. renderBackButton > getLeftTitle > | |
renderLeftButton | function | optional closure to render the left title / buttons element | |
onLeft | function | function will be called when left navBar button is pressed | |
leftButtonImage | Image source | Image for left button | |
leftButtonIconStyle | View style | Image style for left button | |
leftButtonStyle | View style | optional style override for the container of left title / buttons | |
leftButtonTextStyle | Text style | optional style override for the left title element |
Navigation Bar(Right button)
Property | Type | Default | Description |
---|---|---|---|
rightTitle | string | optional string to display on the right. onRight must be provided for this to appear. | |
getRightTitle | function | optional closure to display on the right. onRight must be provided for this to appear. | |
renderRightButton | function | optional closure to render the right title / buttons element | |
onRight | function | function will be called when right navBar button is pressed | |
rightButtonImage | Image source | Image for right button | |
rightButtonIconStyle | View style | Image style for right button | |
rightButtonStyle | View style | optional style override for the container of right title / buttons | |
rightButtonTextStyle | Text style | optional style override for the right title element |
官方地址
https://github.com/aksonov/react-native-router-flux
官方API
https://github.com/aksonov/react-native-router-flux/blob/master/docs/API.md
參考文獻(xiàn)
http://www.cnblogs.com/yz1311/p/6086234.html
http://blog.csdn.net/jiecsdn/article/details/58652511
http://blog.csdn.net/jiecsdn/article/details/58721919
http://blog.csdn.net/jiecsdn/article/details/59057026