Skip to content

Instantly share code, notes, and snippets.

@vincicat
Created April 4, 2017 16:37
Show Gist options
  • Save vincicat/94d4a0db14321fc8c3f73429d1bba523 to your computer and use it in GitHub Desktop.
Save vincicat/94d4a0db14321fc8c3f73429d1bba523 to your computer and use it in GitHub Desktop.
React Native Router Flux - Little Guide on Tabs

Intro

This is one of the great navigation library, just with too little doc on customization, and unfortunately tab is one of them.

Example

<Scene key="myTabBar" tabs={true} hideNavBar tabBarStyle={style.tabBarStyle}>
  <Scene 
    key="myTab" 
    title="My Tab" 
    icon={MyTabIcon} 
   >
      <Scene key="myTab_1" component={MyTabComponent} />
      <Scene key="myTab_2" component={MyTabComponent} />
  </Scene>
</Scene>

Reference In the following, Tab refer to <Scene> with icon , Child refer to <Scene> wrapped by Tab Tabtree/tabbar refer to Tab's parent <Scene> with tabs=true: <Scene tabs=true>...</Scene>

Not so well mentioned behaviour

  1. Tab must has at least 1 Child to enable its own navigation stack
  2. Default Actions' behaviour (or scene type) of Tab is JUMP
  3. If you want to intercrept Tab tapping or changing the default behaviour, write your own onPress function
  <Scene 
    key="myTab" 
    title="My Tab" 
    icon={MyTabIcon} 
    onPress={()=> {
     ...
    }}
   >
  1. You should call the the Tab key in Actions.KEY in order trigger JUMP behaviour when using your own onPress function or from elsewhere, not Child key. (See inner routing)
Actions.myTab({propsToPass})
  1. To "reset" the Tab's history and back to the first Child, use
 Actions.myTab_1({type: ActionConst.PUSH_OR_POP, refresh:{propsToPass} });
  1. To reuse scenes in tab, put them outside the Tabbar, with props clone=true
//under same root
<Scene key="myTabBar" tabs={true} hideNavBar tabBarStyle={style.tabBarStyle}>
  <Scene 
    key="myTab" 
    title="My Tab" 
    icon={MyTabIcon} 
    onPress={()=> {
      Actions.myTab_1({type: ActionConst.REFRESH});
    }}
   >
      <Scene key="myTab_1" component={MyTabComponent} hideNavBar/>
  </Scene>
</Scene>
<Scene key="info" clone ... />
  1. No RNRF lifecycle method is called during switching to mounted tab except props changed.
    • no componentWillUnmount() called by RNRF
    • no componentWillMount() will be called again by RNRF
    • no componentDidMount() will be called again by RNRF
    • At most of the time constructor() will be called only when new instance is created (first load or clonable scene)

Common Tweaking

  1. Do something onMount or unMount of Child => seek alternative
  2. Intercept => See Detailed Example) for reducer demo
  3. Different OS tab behaviour:
    • iOS: static tabbar, tab switching will not destroy the navigation stack and scroll position, but tapping the icon will reset the navigation stack
    • Andriod: tabbar may hide (TBC)
@ameyabits
Copy link

could you please tell me how to get a navbar and tabs at the same time on same page

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment