Skip to content

Instantly share code, notes, and snippets.

@kiok46
Last active June 25, 2017 09:30
Show Gist options
  • Save kiok46/eb446d86210707e836603258528fcf3c to your computer and use it in GitHub Desktop.
Save kiok46/eb446d86210707e836603258528fcf3c to your computer and use it in GitHub Desktop.
Creating CustomTabs in React-Navigation

Custom Tabs

To create Custom tabs in React-Navigation

A structure to get the idea of what we want.

StackNavigator
    - Screen
        - CustomTab | CustomTab
    - Screen
    - Screen
        - CustomTab | CustomTab

These are the helpers that will assist to make a custom navigation, import them.

import {
  createNavigator,
  createNavigationContainer,
  TabRouter,
  addNavigationHelpers,
} from 'react-navigation';
  • createNavigationContainer accepts a createNavigator which take Router and a View.
 const HistoryScreen = createNavigationContainer(
  createNavigator(HistoryTabRouter)(HistoryTabView)
);
  • Create a Router.
import {
 TabRouter,
} from 'react-navigation';
import RecentSearchesTab from './RecentSearchesTab';
import RecentStoriesTab from './RecentStoriesTab';

const HistoryTabRouter = TabRouter(
 {
   RecentSearches: {
     screen: RecentSearchesTab,
     path: '',
   },
   RecentStories: {
     screen: RecentStoriesTab,
     path: 'RecentStories',
   }
 },
 {
   initialRouteName: 'RecentSearches',
 }
);

export default HistoryTabRouter;

  • Create View and link it to the screens.
class HistoryTabBar extends Component {
  constructor(props) {
      super(props)
      const { routes } = this.props.navigation.state
      const navigation = this.props.navigation
  }
  render () {
      return (
          <View style={styles.tabContainer}>
            {this.props.navigation.state.routes.map(route => (
              <TouchableOpacity
                onPress={() => this.props.navigation.navigate(route.routeName)}
                style={styles.tab}
                key={route.routeName}
              >
                <Text>{route.routeName}</Text>
              </TouchableOpacity>
            ))}
          </View>
      );
  }
}


const HistoryTabView = ({ router, navigation }) => {
const { routes, index } = navigation.state;
const ActiveScreen = router.getComponentForState(navigation.state);
return (
  <View >
    <HistoryTabBar navigation={navigation} />
    <ActiveScreen
      navigation={addNavigationHelpers({
        ...navigation,
        state: routes[index],
      })}
    />
  </View>
);
};

Cool!

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

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy