React Native View Pager with 3 Different Indicators

React Native View Pager

This is an example of React Native View Pager with 3 Different Indicators. React Native View Pager allows the user to flip left and right through pages. In our ViewPager Example, we’ll implement a ViewPager that swipes through three different views with the indicator.

  1. PagerDotIndicator
  2. PagerTabIndicator
  3. PagerTitleIndicator

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependency

To use PagerTabIndicator, IndicatorViewPager, PagerTitleIndicator, PagerDotIndicator you need to install rn-viewpager package. To install this

Open the terminal and jump into your project

cd Projectname

Run the following command

npm install rn-viewpager --save

This command will copy all the dependencies into your node_module directory, You can find the directory in node_module the directory named rn-viewpager.

–save is optional, it is just to update the react-navigation dependency in your package.json file.

Code for React Native View Pager

Now Open App.js in any code editor and replace the code with the following code

App.js

//This is an example code for ViewPager//
import React, { Component } from 'react';
//import react in our code.
import { StyleSheet, View, Text } from 'react-native';
// import all basic components
import {
  PagerTabIndicator,
  IndicatorViewPager,
  PagerTitleIndicator,
  PagerDotIndicator,
} from 'rn-viewpager';
//import PagerTabIndicator , IndicatorViewPager , PagerTitleIndicator , PagerDotIndicator

export default class App extends Component {
  render() {
    return (
      <View style={{ flex: 1, marginTop: 30 }}>
        {/*Example of Dot Indicator*/}
        <IndicatorViewPager
          style={{ height: 200 }}
          indicator={this._renderDotIndicator()}>
          {/*_renderDotIndicator() will return the PagerDotIndicator*/}
          <View
            style={{
              backgroundColor: '#C70039',
              flex: 1,
              justifyContent: 'center',
              alignItems: 'center',
            }}>
            <Text style={{ color: 'white', fontSize: 30 }}>One</Text>
          </View>
          <View
            style={{
              backgroundColor: '#FF5733',
              flex: 1,
              justifyContent: 'center',
              alignItems: 'center',
            }}>
            <Text style={{ color: 'white', fontSize: 30 }}>Two</Text>
          </View>
          <View
            style={{
              backgroundColor: '#FFC300',
              flex: 1,
              justifyContent: 'center',
              alignItems: 'center',
            }}>
            <Text style={{ color: 'white', fontSize: 30 }}>Three</Text>
          </View>
        </IndicatorViewPager>
        {/*Example of Title Indicator*/}
        <IndicatorViewPager
          style={{ flex: 1, paddingTop: 20, backgroundColor: 'white' }}
          indicator={this._renderTitleIndicator()}>
          {/*_renderTitleIndicator() will return the PagerTitleIndicator*/}
          <View
            style={{
              backgroundColor: '#C70039',
              flex: 1,
              justifyContent: 'center',
              alignItems: 'center',
            }}>
            <Text style={{ color: 'white', fontSize: 30 }}>One</Text>
          </View>
          <View
            style={{
              backgroundColor: '#FF5733',
              flex: 1,
              justifyContent: 'center',
              alignItems: 'center',
            }}>
            <Text style={{ color: 'white', fontSize: 30 }}>Two</Text>
          </View>
          <View
            style={{
              backgroundColor: '#FFC300',
              flex: 1,
              justifyContent: 'center',
              alignItems: 'center',
            }}>
            <Text style={{ color: 'white', fontSize: 30 }}>Three</Text>
          </View>
        </IndicatorViewPager>
        {/*Example of Tab Indicator*/}
        <IndicatorViewPager
          style={{ flex: 1, paddingTop: 20, backgroundColor: 'white' }}
          indicator={this._renderTabIndicator()}>
          {/*_renderTabIndicator() will return the PagerTabIndicator*/}
          <View
            style={{
              backgroundColor: '#C70039',
              flex: 1,
              justifyContent: 'center',
              alignItems: 'center',
            }}>
            <Text style={{ color: 'white', fontSize: 30 }}>One</Text>
          </View>
          <View
            style={{
              backgroundColor: '#FF5733',
              flex: 1,
              justifyContent: 'center',
              alignItems: 'center',
            }}>
            <Text style={{ color: 'white', fontSize: 30 }}>Two</Text>
          </View>
          <View
            style={{
              backgroundColor: '#FFC300',
              flex: 1,
              justifyContent: 'center',
              alignItems: 'center',
            }}>
            <Text style={{ color: 'white', fontSize: 30 }}>Three</Text>
          </View>
        </IndicatorViewPager>
      </View>
    );
  }
  _renderTitleIndicator() {
    return <PagerTitleIndicator titles={['One', 'Two', 'Three']} />;
  }
  _renderDotIndicator() {
    return <PagerDotIndicator pageCount={3} />;
  }
  _renderTabIndicator() {
    let tabs = [
      {
        text: 'One',
      },
      {
        text: 'Two',
      },
      {
        text: 'Three',
      },
    ];
    return <PagerTabIndicator tabs={tabs} />;
  }
}

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

Output Screenshots

Output in Online Emulator

That was the React Native ViewPager. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂


Related Posts

17 thoughts on “React Native View Pager with 3 Different Indicators”

  1. Thanks fr the article, really useful. Is there a way that you can have a ‘next’ button which would implement the same change as a swipe? Thanks in advance 🙂

    Reply
  2. Hello,

    I’m new in React native i had integrated rn-viewPager from your demo and notes. but when i try to run app. it shows me error like componentWillUpdate is deprecated can you please help me to sort-out this warning.

    Reply
    • In the latest React Native 0.60 version you will see Warning: componentWillUpdate is deprecated and will be removed in the next major version. Use componentDidUpdate instead. As a temporary workaround, you can rename to UNSAFE_componentWillUpdate.

      It is just a warning for now so wait till the library manager updates the library.

      Reply
  3. I Have to make a home activity in which side navigation drawer and slider exist, for slider I have to make dynamic IndicatoViewPager with dynamic PagerDotIndicator,
    could you please help.

    Thanks..

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.