API and Configuration

November 26, 2016 ยท View on GitHub

Available imports

  • Router
  • Scene
  • Modal
  • TabBar
  • getInitialState
  • Reducer
  • DefaultRenderer
  • Switch
  • Actions
  • ActionConst
  • NavBar

Router:

PropertyTypeDefaultDescription
reducerfunctionoptional user-defined reducer for scenes, you may want to use it to intercept all actions and put your custom logic
createReducerfunctionfunction that returns a reducer function for {initialState, scenes} param, you may wrap Reducer(param) with your custom reducer, check Flux usage section below
other propsall properties that will be passed to all your scenes
childrenrequired (if no scenes property passed)Scene root element
scenesobjectoptionalscenes for Router created with Actions.create. This will allow to create all actions BEFORE React processing. If you don't need it you may pass Scene root element as children
getSceneStylefunctionoptionalOptionally override the styles for NavigationCard's Animated.View rendering the scene.
backAndroidHandlerfunctionoptionalOptionally override the handler for BackAndroid, return true to stay in the app or return false to exit the app. Default handler will pop a scene and exit the app at last when the back key is pressed on Android.
onBackAndroidfunctionoptionalGet called after back key is pressed and a scene is poped, won't affect the default behavior.
onExitAppfunctionoptionalOptionally override the default action after back key is pressed on root scene. Return true to stay, or return false to exit the app.

Scene:

PropertyTypeDefaultDescription
keystringrequiredWill be used to call screen transition, for example, Actions.name(params). Must be unique.
componentReact.Componentsemi-requiredThe Component to be displayed. Not required when defining a nested Scene, see example. If it is defined for 'container' scene, it will be used as custom container renderer
initialboolfalseSet to true if this is the initial scene
typestringActionConst.PUSH or ActionConst.JUMPDefines how the new screen is added to the navigator stack. One of ActionConst.PUSH, ActionConst.JUMP, ActionConst.REPLACE, ActionConst.RESET. If parent container is tabbar (tabs=true), ActionConst.JUMP will be automatically set.
cloneboolScenes marked with clone will be treated as templates and cloned into the current scene's parent when pushed. See example.
passPropsboolfalsePass all own props (except style, key, name, component, tabs) to children. Note that passProps is also passed to children.

ActionConst:

We accept shorthand string literal when defining scene type or action params, like:

Actions.ROUTE_NAME({type: 'reset'});
<Scene key="myscene" type="replace" >

but it will be converted to const value when pass to reducer. RECOMMENDATION is to always use const instead of string literal for consistency:

Actions.ROUTE_NAME({type: ActionConst.RESET});
<Scene key="myscene" type={ActionConst.REPLACE} >
PropertyTypeValueShorthand
ActionConst.JUMPstring'REACT_NATIVE_ROUTER_FLUX_JUMP''jump'
ActionConst.PUSHstring'REACT_NATIVE_ROUTER_FLUX_PUSH''push'
ActionConst.REPLACEstring'REACT_NATIVE_ROUTER_FLUX_REPLACE''replace'
ActionConst.BACKstring'REACT_NATIVE_ROUTER_FLUX_BACK''back'
ActionConst.BACK_ACTIONstring'REACT_NATIVE_ROUTER_FLUX_BACK_ACTION''BackAction'
ActionConst.POP_AND_REPLACEstring'REACT_NATIVE_ROUTER_FLUX_POP_AND_REPLACE''popAndReplace'
ActionConst.POP_TOstring'REACT_NATIVE_ROUTER_FLUX_POP_TO''popTo'
ActionConst.REFRESHstring'REACT_NATIVE_ROUTER_FLUX_REFRESH''refresh'
ActionConst.RESETstring'REACT_NATIVE_ROUTER_FLUX_RESET''reset'
ActionConst.FOCUSstring'REACT_NATIVE_ROUTER_FLUX_FOCUS''focus'

ActionConst and Scene.type explaination

ActionConst

are just a bunch of constants represent real values of various actions/scene.type to avoid future changes. you can treat it like redux action.

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.

Not every ActionConst can be used the same way ( use as an action or whether it can be set in scene type or not) that's why it's just a bunch of constants to mask the actual values.

Scene.type

Defines how the new screen is added to the navigator stack. One of push, modal, actionSheet, replace, switch, reset transitionToTop. Default is 'push'. And every Scene.type string literal has a mapped constant in ActionConst, it is recommended to always use constant.

replace: tells navigator to replace current route with new route.
actionSheet: shows Action Sheet popup, you must pass callback as callback function.
modal: type inserts its 'component' into route stack after navigator component. It could be used for popup alerts as well for various needed processes before any navigator transitions (like login auth process). it could be dismissed by using Actions.dismiss().
switch: is used for tab screens.
reset: is similar to replace except it unmounts the componets in the navigator stack.
transitionToTop: will reset router stack ['route.name'] and with animation, if route has sceneConfig. eg <Route name="login" schema="modal" component={Login} type="transitionToTop" />

Animation

PropertyTypeDefaultDescription
durationnumberoptional. acts as a shortcut to writing an applyAnimation function with Animated.timing for a given duration (in ms).
directionstring'horizontal'direction of animation horizontal/vertical/leftToRight ('horizontal' will be right to left)
animationstringanimation options when transitioning: 'fade' currently only option
animationStylefunctionoptional interpolation function for scene transitions: animationStyle={interpolationFunction}
applyAnimationfunctionoptional if provided overrides the default spring animation

Gestures

PropertyTypeDefaultDescription
panHandlersobjectoptional, provide null to disable swipe back gesture
getPanHandlersfunctionoptionalOptionally override the gesture handlers for scene

Scene styles

PropertyTypeDefaultDescription
sceneStyleView style{ flex: 1 }optional style override for the Scene's component
getSceneStylefunctionoptionalOptionally override the styles for NavigationCard's Animated.View rendering the scene. Receives first argument of NavigationSceneRendererProps and second argument of {hideNavBar,hideTabBar,isActive} (see Example app).

Tabs

PropertyTypeDefaultDescription
tabsboolfalseDefines 'TabBar' scene container, so child scenes will be displayed as 'tabs'. If no component is defined, built-in TabBar is used as renderer. All child scenes are wrapped into own navbar.
tabBarStyleView styleoptional style override for the Tabs component
tabBarBackgroundImageImage sourceoptional background image for the Tabs component
tabBarIconContainerStyleView styleoptional style override for the View that contains each tab icon
hideTabBarboolfalsehides tab bar for this scene and any following scenes until explicitly reversed (if built-in TabBar component is used as parent renderer)
hideOnChildTabsboolfalsehides tab bar when another tabs scene is added to the navigation stack.
pressOpacitynumber0.2the opacity when clicking on the tab
PropertyTypeDefaultDescription
hideNavBarboolfalsehides the navigation bar for this scene and any following scenes until explicitly reversed
navigationBarStyleView styleoptional style override for the navigation bar
navigationBarBackgroundImageImage sourceoptional background image for the navigation bar
navBarReact.Componentoptional custom NavBar for the scene. Check built-in NavBar of the component for reference
drawerImageImage sourcerequire('./menu_burger.png')Simple way to override the drawerImage in the navBar
PropertyTypeDefaultDescription
titlestringnullThe title to be displayed in the navigation bar
getTitlefunctionoptionalOptionally closure to return a value of the title based on state
renderTitlefunctionoptionalOptionally closure to render the title
titleStyleText styleoptional style override for the title element
titleOpacitystringoptionalSet opacity for the title in navigation bar
titlePropsobjectnullAny other properties to be set on the title component
PropertyTypeDefaultDescription
backTitlestringoptional string to display with back button
renderBackButtonfunctionoptional closure to render back text or button if this route happens to be the previous route
backButtonImageImage sourcerequire('./back_chevron.png')Simple way to override the back button in the navBar
backButtonTextStyleText styleoptional style override for the back title element
hideBackImagebooleanfalseno default back button image will be displayed
onBackfunctionActions.popactions for back button
PropertyTypeDefaultDescription
leftTitlestringoptional string to display on the left if the previous route does not provide renderBackButton prop. renderBackButton > leftTitle > <previous route's title>
getLeftTitlefunctionoptional closure to display on the left if the previous route does not provide renderBackButton prop. renderBackButton > getLeftTitle > <previous route's title>
renderLeftButtonfunctionoptional closure to render the left title / buttons element
onLeftfunctionfunction will be called when left navBar button is pressed
leftButtonImageImage sourceImage for left button
leftButtonIconStyleView styleImage style for left button
leftButtonStyleView styleoptional style override for the container of left title / buttons
leftButtonTextStyleText styleoptional style override for the left title element
PropertyTypeDefaultDescription
rightTitlestringoptional string to display on the right. onRight must be provided for this to appear.
getRightTitlefunctionoptional closure to display on the right. onRight must be provided for this to appear.
renderRightButtonfunctionoptional closure to render the right title / buttons element
onRightfunctionfunction will be called when right navBar button is pressed
rightButtonImageImage sourceImage for right button
rightButtonIconStyleView styleImage style for right button
rightButtonStyleView styleoptional style override for the container of right title / buttons
rightButtonTextStyleText styleoptional style override for the right title element
...other propsall properties that will be passed to your component instance