component

January 13, 2021 ยท View on GitHub

Starting with version 2.4.0, a customizable <Pagination /> component has been added. This is how it looks like with its default configuration:

react-native-snap-carousel pagination

Table of contents

  1. Props
  2. Note on dots' colors
  3. Usage

Props

PropDescriptionTypeDefault
activeDotIndexIndex of the currently active dotNumberRequired
dotsLengthNumber of dots to displayNumberRequired
activeOpacityOpacity of the dot when tapped. The prop has no effect if tappableDots hasn't been set to true.Number1
carouselRefReference to the Carousel component to which pagination is linked. Needed only when setting tappableDots to true.Objectundefined
containerStyleStyle for dots' container that will be merged with the default oneView Style Object{}
dotColorBackground color of the active dot. Use this if you want to animate the change between active and inactive colors, and always in conjunction with inactiveDotColor (see notes).Stringundefined
dotContainerStyleStyle of each dot's container. Use this if you need to specify styles that wouldn't have any effect when defined with dotStyle (such as flex).View Style Object{}
dotElementOptional custom active dot element that will replace the default one. The element will receive a prop active set to true as well as a prop index.React elementundefined
dotStyleDots' style that will be merged with the default oneView Style Object{}
inactiveDotColorBackground color of the inactive dots. Use this if you want to animate the change between active and inactive colors, and always in conjunction with dotColor (see notes).Stringundefined
inactiveDotElementOptional custom inactive dot element that will replace the default one. The element will receive a prop active set to false as well as a prop indexReact elementundefined
inactiveDotOpacityValue of the opacity effect applied to inactive dotsNumber0.5
inactiveDotScaleValue of the 'scale' transform applied to inactive dotsNumber0.5
inactiveDotStyleDots' style that will be applied to inactive elementsView Style Object{}
renderDotsFunction that gives you complete control over pagination's rendering. It will receive three parameters : (activeIndex, total, context). This can be especially useful in order to replace dots with numbers. :warning: You will need to provide your own logic to handle taps. See this comment for more info.Functionundefined
tappableDotsMake default dots tappable, e.g. your carousel will slide to the corresponding item. Note that carouselRef must be specified for this to work.Booleanfalse
verticalWhether to layout dots vertically or horizontallyBooleanfalse
animatedDurationLength of dot animation (milliseconds)Number250
animatedFrictionControls "bounciness"/overshoot on dot animationNumber4
animatedTensionControls speed dot animationNumber50
delayPressInDotDelay in ms, from the start of the touch, before onPressIn is called on dotNumber0

Note on dots' colors

If your active and inactive dots aren't of the same color, you have a choice to make:

  1. either animate the color transition by specifying both dotColor and inactiveDotColor
  2. or setting { backgroundColor } in both dotStyle and inactiveDotStyle.

When animating the color transition, the dot component will no longer be able to use the native driver for scale and opacity transitions. As stated in React Native's doc, color animations aren't supported by the native driver. And, unfortunately, it doesn't seem currently possible to run native-powered and js-powered animations at the same time on the same element.

Basically, this is a tradeoff between color transition and optimal smoothness. We recommended you to try the first version and, if you experiment performance drops, to settle for the second one.

Usage

Since <Pagination /> is, purposely, a separated component, you need to connect it to your <Carousel /> component manually. This is pretty straightforward, but here is an example to get you started.

import Carousel, { Pagination } from 'react-native-snap-carousel';

export default class MyCarousel extends Component {

    _renderItem ({item, index}) {
        return <MySlideComponent data={item} />
    }

    get pagination () {
        const { entries, activeSlide } = this.state;
        return (
            <Pagination
              dotsLength={entries.length}
              activeDotIndex={activeSlide}
              containerStyle={{ backgroundColor: 'rgba(0, 0, 0, 0.75)' }}
              dotStyle={{
                  width: 10,
                  height: 10,
                  borderRadius: 5,
                  marginHorizontal: 8,
                  backgroundColor: 'rgba(255, 255, 255, 0.92)'
              }}
              inactiveDotStyle={{
                  // Define styles for inactive dots here
              }}
              inactiveDotOpacity={0.4}
              inactiveDotScale={0.6}
            />
        );
    }

    render () {
        return (
            <View>
                <Carousel
                  data={this.state.entries}
                  renderItem={this._renderItem}
                  onSnapToItem={(index) => this.setState({ activeSlide: index }) }
                />
                { this.pagination }
            </View>
        );
    }