Newby Coder header banner

React Native Tabs

Adding Tabs in a React Native Application

Tabs are used to group content

react-native-scrollable-tab-view package provides ScrollableTabView and ScrollableTabBar

These can be used to implement tabs which can be scrolled like a viewpager and along with the tab bar

Creating new React Native App

Check React Native Installation for installation related info

Use react-native init command to create a new React Native project (here named NcTabsApp)

react-native init NcTabsApp

This creates a directory named NcTabsApp and initializes it as a react native application directory

This can be skipped in case an existing react native project is being integrated into


Dependency

Go to project directory (here NcTabsApp)

cd NcTabsApp

Add react-native-scrollable-tab-view package to current project using yarn

yarn add react-native-scrollable-tab-view

or using npm

npm install react-native-scrollable-tab-view --save

iOS

After yarn add command, cd into ios folder inside project directory

cd ios

Use pod command to install any required CocoaPods dependencies:

pod install

Usage

Example usage of ScrollableTabView:

<ScrollableTabView
    style={{ marginTop: 20 }}
    initialPage={0}
    renderTabBar={() => <ScrollableTabBar />}
  >
    <Text tabLabel='Tab #1'>Eat</Text>
    <View style={styles.tab2} tabLabel='Tab #2 word word'><Text>Sleep</Text></View>
    <Text tabLabel='Tab #3 word word word'>Code</Text>
    <Tab4 tabLabel='Tab #4 word word word word'><Text>Sleep</Text> </Tab4>
    <Image tabLabel='Tab #5' style={styles.image}  source={{uri:"https://resourceplus.files.wordpress.com/2015/01/image.jpg"}} />
</ScrollableTabView>

Some of its props are

Props

Except first parameter, others are optional

Following are some cross-platform properties of Modal component

Properties other than onRequestClose are optional

Import
import ScrollableTabView, { ScrollableTabBar } from 'react-native-scrollable-tab-view';

App Code

App.js
import React from 'react';
import {
  Text,
  View,
  Image,
} from 'react-native';
import ScrollableTabView, { ScrollableTabBar } from 'react-native-scrollable-tab-view';

export default App = () => {
  return <ScrollableTabView
    style={{ marginTop: 20 }}
    initialPage={0}
    renderTabBar={() => <ScrollableTabBar />}
  >
    <Text tabLabel='Tab #1'>Eat</Text>
    <View style={styles.tab2} tabLabel='Tab #2 word word'><Text>Sleep</Text></View>
    <Text tabLabel='Tab #3 word word word'>Code</Text>
    <Tab4 tabLabel='Tab #4 word word word word'><Text>Sleep</Text> </Tab4>
    <Image tabLabel='Tab #5' style={styles.image}  source={{uri:"https://resourceplus.files.wordpress.com/2015/01/image.jpg"}} />
  </ScrollableTabView>;
}

class Tab4 extends React.Component {
  render() {
    return (
      <View>
        <Image style={styles.image} source={{uri: "https://eatsleepmeme.com/wp-content/uploads/2020/06/thumb_none-can-have-shi-by-ztfares-more-memes-72554285.png"}} />
      </View>
    );
  }
}

const styles = {
    tab2: {
      backgroundColor: 'orange',
      alignItems: 'center',
      justifyContent: 'center',
      height: '100%'
    },
    image: {
      resizeMode: 'stretch',
      width: '100%',
      height: '100%'
    }
}

Run instructions

Running in Android

cd into project directory (here NcTabsApp)

cd NcTabsApp

Run metro server to serve js

react-native start

Go to project directory in another terminal tab

Enter following run command for Android:

react-native run-android
cl-react-native-tabs-app

Running in ios

cd into project directory (here NcTabsApp)

cd NcTabsApp

Enter following command :

react-native run-ios

This might take some time


If the app shows error about metro server not configured (check Running an App in iOS), then run metro server in another tab:

react-native start

Reload the app

Re-run react-native run-ios command if reloading doesn't work

cm-react-native-tabs-as1cm-react-native-tabs-as2cm-react-native-tabs-as3