Convert Figma logo to code with AI

leecade logoreact-native-swiper

The best Swiper component for React Native.

10,413
2,342
10,413
787

Top Related Projects

39,745

Most modern mobile touch slider with hardware accelerated transitions

Swiper/carousel component for React Native featuring previews, multiple layouts, parallax images, performant handling of huge numbers of items, and more. Compatible with Android & iOS.

UI Components Library for React Native

React Native wrapper for the Android ViewPager and iOS UIPageViewController.

React Native wrapper for the Android ViewPager and iOS UIPageViewController.

Quick Overview

React Native Swiper is a popular and customizable swiper component for React Native applications. It provides a smooth, gesture-based swiping experience for creating carousels, image galleries, and other swipeable content in mobile apps.

Pros

  • Easy to integrate and use in React Native projects
  • Highly customizable with numerous props and options
  • Supports both horizontal and vertical swiping
  • Includes built-in pagination and navigation buttons

Cons

  • Some users report performance issues with large numbers of slides
  • Documentation could be more comprehensive and up-to-date
  • Occasional compatibility issues with newer React Native versions
  • Limited built-in animation options

Code Examples

  1. Basic usage:
import Swiper from 'react-native-swiper'

<Swiper>
  <View style={styles.slide1}>
    <Text style={styles.text}>Hello Swiper</Text>
  </View>
  <View style={styles.slide2}>
    <Text style={styles.text}>Beautiful</Text>
  </View>
  <View style={styles.slide3}>
    <Text style={styles.text}>And simple</Text>
  </View>
</Swiper>
  1. Customizing pagination:
<Swiper
  showsPagination={true}
  paginationStyle={{bottom: 10}}
  dot={<View style={{backgroundColor: 'rgba(0,0,0,.2)', width: 8, height: 8, borderRadius: 4, marginLeft: 3, marginRight: 3, marginTop: 3, marginBottom: 3}} />}
  activeDot={<View style={{backgroundColor: '#007aff', width: 8, height: 8, borderRadius: 4, marginLeft: 3, marginRight: 3, marginTop: 3, marginBottom: 3}} />}
>
  {/* Slides */}
</Swiper>
  1. Auto-playing swiper:
<Swiper
  autoplay={true}
  autoplayTimeout={3}
  loop={true}
>
  {/* Slides */}
</Swiper>

Getting Started

  1. Install the package:

    npm install react-native-swiper --save
    
  2. Import and use in your React Native component:

    import Swiper from 'react-native-swiper'
    
    export default function App() {
      return (
        <Swiper>
          <View style={styles.slide1}>
            <Text>Slide 1</Text>
          </View>
          <View style={styles.slide2}>
            <Text>Slide 2</Text>
          </View>
          <View style={styles.slide3}>
            <Text>Slide 3</Text>
          </View>
        </Swiper>
      )
    }
    
  3. Customize the Swiper component using props as needed for your specific use case.

Competitor Comparisons

39,745

Most modern mobile touch slider with hardware accelerated transitions

Pros of Swiper

  • More comprehensive and feature-rich, supporting various platforms beyond React Native
  • Better documentation and extensive API options
  • Larger community and more frequent updates

Cons of Swiper

  • Larger file size and potentially heavier performance impact
  • May require additional setup for React Native projects
  • Steeper learning curve due to more complex API

Code Comparison

React Native Swiper:

<Swiper style={styles.wrapper} showsButtons={true}>
  <View style={styles.slide1}><Text style={styles.text}>Hello Swiper</Text></View>
  <View style={styles.slide2}><Text style={styles.text}>Beautiful</Text></View>
  <View style={styles.slide3}><Text style={styles.text}>And simple</Text></View>
</Swiper>

Swiper:

<Swiper
  spaceBetween={50}
  slidesPerView={3}
  onSlideChange={() => console.log('slide change')}
  onSwiper={(swiper) => console.log(swiper)}
>
  <SwiperSlide>Slide 1</SwiperSlide>
  <SwiperSlide>Slide 2</SwiperSlide>
  <SwiperSlide>Slide 3</SwiperSlide>
</Swiper>

The code comparison shows that Swiper offers more configuration options out of the box, while React Native Swiper has a simpler API tailored for React Native applications. Swiper's implementation allows for more customization, such as setting the number of slides per view and defining callback functions for various events.

Swiper/carousel component for React Native featuring previews, multiple layouts, parallax images, performant handling of huge numbers of items, and more. Compatible with Android & iOS.

Pros of react-native-snap-carousel

  • More customizable with a wide range of props and options
  • Better performance, especially for large lists
  • Supports vertical carousels out of the box

Cons of react-native-snap-carousel

  • Slightly more complex API, which may require a steeper learning curve
  • Less frequently updated compared to react-native-swiper

Code Comparison

react-native-snap-carousel:

<Carousel
  data={entries}
  renderItem={renderItem}
  sliderWidth={sliderWidth}
  itemWidth={itemWidth}
  loop={true}
  autoplay={true}
/>

react-native-swiper:

<Swiper
  showsButtons={true}
  autoplay={true}
  loop={true}
>
  {slides.map((slide, index) => (
    <View key={index}>{slide}</View>
  ))}
</Swiper>

react-native-snap-carousel offers more granular control over item dimensions and rendering, while react-native-swiper has a simpler API for basic carousel functionality. react-native-snap-carousel is generally preferred for more complex carousel implementations, while react-native-swiper is suitable for simpler use cases and quicker setup.

UI Components Library for React Native

Pros of react-native-ui-lib

  • Comprehensive UI component library with a wide range of pre-built elements
  • Consistent design system and theming capabilities
  • Active development and regular updates

Cons of react-native-ui-lib

  • Larger package size due to the extensive component library
  • Steeper learning curve for developers new to the library
  • May require more configuration for custom styling

Code Comparison

react-native-ui-lib:

import { View, Text, Button } from 'react-native-ui-lib';

const MyComponent = () => (
  <View>
    <Text>Hello, World!</Text>
    <Button label="Click me" onPress={() => {}} />
  </View>
);

react-native-swiper:

import Swiper from 'react-native-swiper';

const MySwiper = () => (
  <Swiper>
    <View><Text>Slide 1</Text></View>
    <View><Text>Slide 2</Text></View>
    <View><Text>Slide 3</Text></View>
  </Swiper>
);

react-native-ui-lib offers a more comprehensive set of UI components and a consistent design system, making it suitable for larger projects with complex UI requirements. However, it comes with a larger package size and potentially more setup time.

react-native-swiper is focused solely on providing swiper functionality, making it lightweight and easy to implement for projects that only need this specific feature. It has a smaller footprint but lacks the extensive UI toolkit provided by react-native-ui-lib.

React Native wrapper for the Android ViewPager and iOS UIPageViewController.

Pros of react-native-pager-view

  • Uses native components for better performance and smoother animations
  • Supports both horizontal and vertical paging
  • Offers more customization options and advanced features

Cons of react-native-pager-view

  • Slightly more complex setup and configuration
  • May have a steeper learning curve for beginners
  • Requires additional steps for web support

Code Comparison

react-native-pager-view:

import PagerView from 'react-native-pager-view';

<PagerView style={styles.pagerView} initialPage={0}>
  <View key="1"><Text>First page</Text></View>
  <View key="2"><Text>Second page</Text></View>
</PagerView>

react-native-swiper:

import Swiper from 'react-native-swiper';

<Swiper style={styles.wrapper} showsButtons={true}>
  <View style={styles.slide1}><Text>First slide</Text></View>
  <View style={styles.slide2}><Text>Second slide</Text></View>
</Swiper>

Both libraries provide similar functionality for creating swipeable views in React Native applications. react-native-pager-view offers better performance and more advanced features, but may be more challenging for beginners. react-native-swiper is simpler to use and set up, but may not provide the same level of performance or customization options. The choice between the two depends on the specific requirements of your project and your familiarity with React Native development.

React Native wrapper for the Android ViewPager and iOS UIPageViewController.

Pros of react-native-pager-view

  • Uses native components for better performance and smoother animations
  • Supports both horizontal and vertical paging
  • Offers more customization options and advanced features

Cons of react-native-pager-view

  • Slightly more complex setup and configuration
  • May have a steeper learning curve for beginners
  • Requires additional steps for web support

Code Comparison

react-native-pager-view:

import PagerView from 'react-native-pager-view';

<PagerView style={styles.pagerView} initialPage={0}>
  <View key="1"><Text>First page</Text></View>
  <View key="2"><Text>Second page</Text></View>
</PagerView>

react-native-swiper:

import Swiper from 'react-native-swiper';

<Swiper style={styles.wrapper} showsButtons={true}>
  <View style={styles.slide1}><Text>First slide</Text></View>
  <View style={styles.slide2}><Text>Second slide</Text></View>
</Swiper>

Both libraries provide similar functionality for creating swipeable views in React Native applications. react-native-pager-view offers better performance and more advanced features, but may be more challenging for beginners. react-native-swiper is simpler to use and set up, but may not provide the same level of performance or customization options. The choice between the two depends on the specific requirements of your project and your familiarity with React Native development.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

react-native-swiper

The best Swiper component for React Native.

JavaScript Style Guide npm version npm version PR Stats Issue Stats Join the chat

react-native-swiper

Roadmap

see: ROADMAP.md

Changelogs

  • [1.6.0-rc]
    • Dependency
      • Remove ViewPagerAndroid, use ScrollView #1009
    • Test Integration
      • Setup e2e test
    • TypeScript
      • correct the wrong types #1000
      • Add missing scrollBy TypeScript definition #931
    • New Feature
      • add scrollTo #831
      • Added prop to disable the PrevButton #749
      • Optionally render page #1004
    • Bug Fix
      • ES6 and CommonJS compatibility #717
      • Solves the issue of state messing up when parent component calls setState #939
      • replay when autoplay is setted to true #1002
      • fix broken examples and migrate to react-native 0.60.x
      • fix bad jumping on ios when loadMinimal set true
      • fix fliker when loop and loadMinimal are enabled #1062
  • [1.5.6]

  • [1.5.5]

    • Update: using PropTypes from prop-types and Change View.propTypes to ViewPropTypes
  • [1.5.4]

    • Added easily accessible pagination point manipulation: use dotColor / activeDotColor and dotStyle / activeDotStyle (thanks @denizs)
    • Added scrollEnabled prop to documentation (thanks @ibandominguez)
  • [1.5.3]

    • Add loadMinimalLoader prop to customize <ActivityIndicator /> (thanks @Exilz)
    • Disable autoplay timer when prop changes to false (thanks @dizlexik)
    • Special thanks to @hypatiah for fixed some grammatical errors in README
  • [1.5.2]

    • Add yarn lock
    • Fix jitter when quickly swiping back and forth between pages (iOS) (thanks @nemophrost)
    • The first webview always reloaded when injecting the rest of the children (thanks @eosterberg)

see more: CHANGELOG.md

Show Cases

Try these cases by yourself very easy, Just open examples/ios/swiper.xcodeproj in Xcode, then press Cmd + R; you may edit examples/index.ios.js for switch cases.

examples/components/Basic

examples/components/Swiper

examples/components/SwiperNumber

examples/components/Phone

examples/components/LoadMinimal

Getting Started

Installation

v1.5.14

$ npm i react-native-swiper --save

v1.6.0-rc

npm i --save react-native-swiper@next

Basic Usage

  • Install react-native first
$ npm i react-native-cli -g
  • Initialization of a react-native project
$ react-native init myproject
  • Then, edit myproject/index.ios.js, like this:
import React, { Component } from 'react'
import { AppRegistry, StyleSheet, Text, View } from 'react-native'

import Swiper from 'react-native-swiper'

const styles = StyleSheet.create({
  wrapper: {},
  slide1: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#9DD6EB'
  },
  slide2: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#97CAE5'
  },
  slide3: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#92BBD9'
  },
  text: {
    color: '#fff',
    fontSize: 30,
    fontWeight: 'bold'
  }
})

export default class SwiperComponent extends Component {
  render() {
    return (
      <Swiper style={styles.wrapper} showsButtons={true}>
        <View style={styles.slide1}>
          <Text style={styles.text}>Hello Swiper</Text>
        </View>
        <View style={styles.slide2}>
          <Text style={styles.text}>Beautiful</Text>
        </View>
        <View style={styles.slide3}>
          <Text style={styles.text}>And simple</Text>
        </View>
      </Swiper>
    )
  }
}

AppRegistry.registerComponent('myproject', () => SwiperComponent)

Properties

Basic

PropDefaultTypeDescription
horizontaltrueboolIf true, the scroll view's children are arranged horizontally in a row instead of vertically in a column.
looptrueboolSet to false to disable continuous loop mode.
index0numberIndex number of initial slide.
showsButtonsfalseboolSet to true make control buttons visible.
autoplayfalseboolSet to true enable auto play mode.
onIndexChanged(index) => nullfuncCalled with the new index when the user swiped

Custom basic style & content

PropDefaultTypeDescription
width-numberIf no specify default enable fullscreen mode by flex: 1.
height-numberIf no specify default fullscreen mode by flex: 1.
style{...}styleSee default style in source.
containerStyle{...}styleSee default container style in source.
loadMinimalfalseboolOnly load current index slide , loadMinimalSize slides before and after.
loadMinimalSize1numbersee loadMinimal
loadMinimalLoader<ActivityIndicator />elementCustom loader to display when slides aren't loaded

Pagination

PropDefaultTypeDescription
showsPaginationtrueboolSet to true make pagination visible.
paginationStyle{...}styleCustom styles will merge with the default styles.
renderPagination-functionComplete control how to render pagination with three params (index, total, context) ref to this.state.index / this.state.total / this, For example: show numbers instead of dots.
dot<View style={{backgroundColor:'rgba(0,0,0,.2)', width: 8, height: 8,borderRadius: 4, marginLeft: 3, marginRight: 3, marginTop: 3, marginBottom: 3,}} />elementAllow custom the dot element.
activeDot<View style={{backgroundColor: '#007aff', width: 8, height: 8, borderRadius: 4, marginLeft: 3, marginRight: 3, marginTop: 3, marginBottom: 3,}} />elementAllow custom the active-dot element.
dotStyle-objectAllow custom the dot element.
dotColor-stringAllow custom the dot element.
activeDotColor-stringAllow custom the active-dot element.
activeDotStyle-objectAllow custom the active-dot element.

Autoplay

PropDefaultTypeDescription
autoplaytrueboolSet to true enable auto play mode.
autoplayTimeout2.5numberDelay between auto play transitions (in second).
autoplayDirectiontrueboolCycle direction control.

Control buttons

PropDefaultTypeDescription
showsButtonstrueboolSet to true make control buttons visible.
buttonWrapperStyle{backgroundColor: 'transparent', flexDirection: 'row', position: 'absolute', top: 0, left: 0, flex: 1, paddingHorizontal: 10, paddingVertical: 10, justifyContent: 'space-between', alignItems: 'center'}styleCustom styles.
nextButton<Text style={styles.buttonText}>›</Text>elementAllow custom the next button.
prevButton<Text style={styles.buttonText}>‹</Text>elementAllow custom the prev button.

Props of Children

PropDefaultTypeDescription
style{...}styleCustom styles will merge with the default styles.
title{...}elementIf this parameter is not specified, will not render the title.

Basic props of <ScrollView />

PropDefaultTypeDescription
horizontaltrueboolIf true, the scroll view's children are arranged horizontally in a row instead of vertically in a column.
pagingEnabledtrueboolIf true, the scroll view stops on multiples of the scroll view's size when scrolling. This can be used for horizontal pagination.
showsHorizontalScrollIndicatorfalseboolSet to true if you want to show horizontal scroll bar.
showsVerticalScrollIndicatorfalseboolSet to true if you want to show vertical scroll bar.
bouncesfalseboolIf true, the scroll view bounces when it reaches the end of the content if the content is larger then the scroll view along the axis of the scroll direction. If false, it disables all bouncing even if the alwaysBounce* props are true.
scrollsToTopfalseboolIf true, the scroll view scrolls to top when the status bar is tapped.
removeClippedSubviewstrueboolIf true, offscreen child views (whose overflow value is hidden) are removed from their native backing superview when offscreen. This canimprove scrolling performance on long lists.
automaticallyAdjustContentInsetsfalseboolSet to true if you need adjust content insets automation.
scrollEnabledtrueboolEnables/Disables swiping

@see: http://facebook.github.io/react-native/docs/scrollview.html

Supported ScrollResponder

PropParamsTypeDescription
onScrollBeginDrage / state / contextfunctionWhen animation begins after letting up
onMomentumScrollEnde / state / contextfunctionMakes no sense why this occurs first during bounce
onTouchStartCapturee / state / contextfunctionImmediately after onMomentumScrollEnd
onTouchStarte / state / contextfunctionSame, but bubble phase
onTouchEnde / state / contextfunctionYou could hold the touch start for a long time
onResponderReleasee / state / contextfunctionWhen lifting up - you could pause forever before * lifting

Note: each ScrollResponder be injected with two params: state and context, you can get state and context(ref to swiper's this) from params, for example:

var swiper = React.createClass({
  _onMomentumScrollEnd: function (e, state, context) {
    console.log(state, context.state)
  },
  render: function() {
    return (
      <Swiper style={styles.wrapper}
      onMomentumScrollEnd ={this._onMomentumScrollEnd}
     ...
      </Swiper>
    )
  }
})

More ScrollResponder info, see: https://github.com/facebook/react-native/blob/master/Libraries/Components/ScrollResponder.js

Methods

scrollBy(index, animated)

Scroll by relative index.

Parameters:

NameTypedefaultDescription
indexnumberundefinedoffset index
animatedbooltrueoffset index

Examples

$ cd examples
$ npm i
$ react-native run-ios

Quick start with examples.

Development

$ cd examples
$ yarn
$ yarn start
$ react-native run-ios

Then launch simulator to preview. Note that you just need to edit the source file src/index.js, the change will auto sync to examples.

After development, you should add test for your modification and make all tests passed to prevent other contributors break the feature in the future accidentally. We use detox + jest for e2e test now, you can read Detox for more detail.

Contribution

Questions

Feel free to contact me or create an issue

Inspired by nolimits4web/Swiper & Design material from Dribbble & made with ♥.

NPM DownloadsLast 30 Days