React Native Swiper Component
react-native-swiper is a widely used, feature-rich swiper/carousel component designed for React Native applications. As of its latest stable release, v1.6.0, it provides a highly customizable solution for displaying paginated content, supporting both iOS and Android platforms. The library focuses on providing a smooth user experience with features like autoplay, infinite looping, flexible pagination dots, and lazy loading for performance optimization via `loadMinimal`. It supports TypeScript out-of-the-box, shipping with correct type definitions, and has recently moved away from `ViewPagerAndroid` to `ScrollView` for improved compatibility and reduced dependency footprint. The project maintains an active development pace with consistent bug fixes and new features, though no strict release cadence is formally documented. Its key differentiators include extensive prop-based customization for styling and behavior, robust platform support, and a history of addressing common React Native carousel challenges.
Common errors
-
Uncaught TypeError: _this.refs.scrollView.scrollTo is not a function
cause This error typically occurred in `react-native-swiper` versions prior to 1.5.12 due to issues with accessing the internal `scrollView` ref or its `scrollTo` method.fixUpgrade `react-native-swiper` to version 1.5.12 or newer. Ensure your component structure allows for proper ref forwarding if you are wrapping the `Swiper` component. -
Swiper content not rendering correctly or experiencing layout issues after device rotation on Android.
cause Incorrect calculation of component width and height after orientation changes, particularly on Android, was a known bug in older versions.fixUpgrade `react-native-swiper` to v1.5.13 or newer, which includes fixes for recalculating dimensions correctly on layout changes and device rotations. -
The `index` prop on iOS does not correctly initialize or update the swiper's current page.
cause An issue existed in versions prior to 1.5.10 where the `index` prop was not reliably setting or updating the initial slide on iOS.fixUpgrade `react-native-swiper` to version 1.5.10 or newer to resolve the `index` prop initialization and update issues on iOS.
Warnings
- breaking The `style` prop for the Android swiper container was renamed to `containerStyle` in v1.6.0. Directly applying `style` to the `Swiper` component for its container will no longer work on Android.
- breaking The internal dependency on `ViewPagerAndroid` was removed in v1.6.0, in favor of using React Native's `ScrollView`. This change may require cleaning old `node_modules` and re-installing dependencies.
- gotcha Older versions (prior to v1.5.5) utilized `React.PropTypes`, which has been deprecated in modern React. v1.5.5 updated to use the separate `prop-types` package.
- gotcha Issues were reported in older versions (fixed in v1.6.0-rc) where the Swiper's internal state could be messed up when a parent component frequently calls `setState`, leading to unexpected behavior or visual glitches.
- gotcha Compatibility for ES6 and CommonJS was improved significantly in `v1.6.0-rc`. Older versions might exhibit less robust behavior in mixed module environments or with specific bundler configurations.
Install
-
npm install react-native-swiper -
yarn add react-native-swiper -
pnpm add react-native-swiper
Imports
- Swiper
const Swiper = require('react-native-swiper')import Swiper from 'react-native-swiper'
- SwiperProps
import { SwiperProps } from 'react-native-swiper'import type { SwiperProps } from 'react-native-swiper'
Quickstart
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import Swiper from 'react-native-swiper';
const App = () => {
return (
<Swiper style={styles.wrapper} showsButtons loop autoplay>
<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>
);
};
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 App;