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.
- PagerDotIndicator
- PagerTabIndicator
- 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 thereact-native-cli
command line utility. Open the terminal and go to the workspace and runInstallation 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
1 | cd Projectname |
Run the following command
1 | 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 dependancy in your package.json file.
Dependency Version for this Example Used is
1 | "rn-viewpager": "1.2.9" |
Now Open App.js in any code editor and replace the code with the following code.
App.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | //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.That was the React Native ViewPager. If you have any doubt 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. 🙂
How useful was this post?
Click on a star to rate us!
Average rating / 5. Vote count:
We are sorry that this post was not useful for you!
Let us improve this post!
Thanks for your feedback!
Hi,
How to change the DotIndicator color.
Hi Ravi,
To change the color of the DotIndicator you need to override the default prop using dotStyle and selectedDotStyle where you can change the background color of the dot indicator. Please change the PagerDotIndicator with the following lines and you will see the change.
In case of any issue please comment.
Thankyou so much for your help.
Any time brother. 🙂
Hi Snehal, Can you help me create the View pager with button and the button should be below to the dot-pager.
Actually, we are not clear with the query but still please find the attached file (please change the extension to js) or you can find it snack by clicking on the below link.
https://snack.expo.io/@aboutreact/view-pager-with-button-below-dot-pager
If this is not matching to your requirement then can you please explain it in detail so that we can help you.