Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
react-native-snap-carousel
Advanced tools
Swiper/carousel component for React Native with previews, multiple layouts, parallax images, performant handling of huge numbers of items, and RTL support. Compatible with Android & iOS.
react-native-snap-carousel is a versatile and highly customizable carousel component for React Native. It allows developers to create beautiful and performant carousels with snapping effect, which can be used for image sliders, product showcases, and more.
Basic Carousel
This code demonstrates a basic carousel with three items. Each item is rendered using the `renderItem` function, and the carousel is configured to snap to each item.
import React from 'react';
import { View, Text, Dimensions } from 'react-native';
import Carousel from 'react-native-snap-carousel';
const { width: viewportWidth } = Dimensions.get('window');
const data = [{ title: 'Item 1' }, { title: 'Item 2' }, { title: 'Item 3' }];
const renderItem = ({ item }) => (
<View style={{ backgroundColor: 'lightblue', borderRadius: 5, height: 150, padding: 50 }}>
<Text>{item.title}</Text>
</View>
);
const MyCarousel = () => (
<Carousel
data={data}
renderItem={renderItem}
sliderWidth={viewportWidth}
itemWidth={viewportWidth - 60}
/>
);
export default MyCarousel;
Custom Pagination
This code demonstrates how to add custom pagination to the carousel. The `Pagination` component is used to display dots indicating the current active slide.
import React, { useState } from 'react';
import { View, Text, Dimensions } from 'react-native';
import Carousel, { Pagination } from 'react-native-snap-carousel';
const { width: viewportWidth } = Dimensions.get('window');
const data = [{ title: 'Item 1' }, { title: 'Item 2' }, { title: 'Item 3' }];
const renderItem = ({ item }) => (
<View style={{ backgroundColor: 'lightblue', borderRadius: 5, height: 150, padding: 50 }}>
<Text>{item.title}</Text>
</View>
);
const MyCarousel = () => {
const [activeSlide, setActiveSlide] = useState(0);
return (
<View>
<Carousel
data={data}
renderItem={renderItem}
sliderWidth={viewportWidth}
itemWidth={viewportWidth - 60}
onSnapToItem={(index) => setActiveSlide(index)}
/>
<Pagination
dotsLength={data.length}
activeDotIndex={activeSlide}
containerStyle={{ backgroundColor: 'rgba(0, 0, 0, 0.75)' }}
dotStyle={{ width: 10, height: 10, borderRadius: 5, marginHorizontal: 8, backgroundColor: 'white' }}
inactiveDotStyle={{ backgroundColor: 'gray' }}
inactiveDotOpacity={0.4}
inactiveDotScale={0.6}
/>
</View>
);
};
export default MyCarousel;
Looping Carousel
This code demonstrates a looping carousel with autoplay functionality. The carousel will automatically cycle through the items with a specified delay and interval.
import React from 'react';
import { View, Text, Dimensions } from 'react-native';
import Carousel from 'react-native-snap-carousel';
const { width: viewportWidth } = Dimensions.get('window');
const data = [{ title: 'Item 1' }, { title: 'Item 2' }, { title: 'Item 3' }];
const renderItem = ({ item }) => (
<View style={{ backgroundColor: 'lightblue', borderRadius: 5, height: 150, padding: 50 }}>
<Text>{item.title}</Text>
</View>
);
const MyCarousel = () => (
<Carousel
data={data}
renderItem={renderItem}
sliderWidth={viewportWidth}
itemWidth={viewportWidth - 60}
loop={true}
autoplay={true}
autoplayDelay={1000}
autoplayInterval={3000}
/>
);
export default MyCarousel;
react-native-swiper is another popular package for creating carousels and swipers in React Native. It offers a simple API and supports both horizontal and vertical swiping. Compared to react-native-snap-carousel, it is less customizable but easier to set up for basic use cases.
react-native-reanimated-carousel is a performant carousel component built on top of react-native-reanimated. It provides smooth animations and better performance, especially for complex use cases. It is more advanced and requires a deeper understanding of animations compared to react-native-snap-carousel.
react-native-looped-carousel is a simple and lightweight carousel component for React Native. It supports infinite scrolling and basic customization options. It is less feature-rich compared to react-native-snap-carousel but can be a good choice for simpler carousel needs.
ParallaxImage
componentPagination
component
These are live apps we've created that make heavy use of the plugin. Don't be shy, share yours if you've done something awesome with it!
Hey there,
Creating and maintaining this plugin has been a fun ride that started in 2016. We thank you all for your appreciation and for making the most out of it! You've motivated us to spend countless hours improving the plugin, and made us happy to give back to the Open Source community.
Put simply, we love this project. However we currently aren't able to give it the love it deserves and the care it requires. If you have enough time and knowledge, and want to become a maintainer, please let us know.
We're not abandoning the ship, but we need more people to help us keep it alive and well!
$ npm install --save react-native-snap-carousel
If you're using Typescript you should also install type definitions:
$ npm install --save @types/react-native-snap-carousel
import Carousel from 'react-native-snap-carousel';
export class MyCarousel extends Component {
_renderItem = ({item, index}) => {
return (
<View style={styles.slide}>
<Text style={styles.title}>{ item.title }</Text>
</View>
);
}
render () {
return (
<Carousel
ref={(c) => { this._carousel = c; }}
data={this.state.entries}
renderItem={this._renderItem}
sliderWidth={sliderWidth}
itemWidth={itemWidth}
/>
);
}
}
Here are simple examples that can be edited in real time in your browser:
You can also find a more in-depth (read "complex") one in the /example
folder.
In order to let you to create mighty carousels and to keep up with your requests, we add new features on a regular basis. Consequently, the list of available props has become really huge and deserves a documentation of its own.
In version 3.6.0
, we've added two new layouts on top of the original one: the first one is called 'stack' since it mimics a stack of cards, and the other one is called 'tinder' since it provides a Tinder-like animation.
You can choose between the three of them using the new prop layout
and you can modify the default card offset in the 'stack' and 'tinder' layouts with prop layoutCardOffset
.
<Carousel layout={'default'} />
<Carousel layout={'stack'} layoutCardOffset={`18`} />
<Carousel layout={'tinder'} layoutCardOffset={`9`} />
A few things worth noting:
stack
or tinder
layouts if you have a large data set to display. In order to avoid rendering issues, the carousel will use a ScrollView
component rather than a FlatList
one for those layouts (see prop useScrollView
). The tradeof is that you won't benefit from any of FlatList
's advanced optimizations. See this issue for workarounds; or you may want to implement your own custom interpolation.On top of the new layouts, we've exposed the logic we used so that users can create their own awesome layouts! If you're interested, take a deep breath and dive into the dedicated documentation.
Here are a few examples of what can easily be achieved (you can explore the source code and try it live in the provided example):
ParallaxImage
componentVersion 3.0.0
introduced a <ParallaxImage />
component, an image component aware of carousel's current scroll position and therefore able to display a nice parallax effect (powered by the native driver to ensure top-notch performance).
ParallaxImage
component"Pagination
componentStarting with version 2.4.0
, a customizable <Pagination />
component has been added. You can see below how it looks like with its default configuration.
Pagination
component"We've gathered together all the useful tips and tricks. There is a bunch of them, which makes this section a must-read!
Make sure to read about the known issues before opening a new one; you may find something useful.
Android's debug mode is a mess: timeouts regularly desynchronize and scroll events are fired with some lag, which completely alters the inner logic of the carousel. On Android, you will experience issues with carousel's behavior when JS Dev Mode is enabled, and you might have trouble with unreliable callbacks and loop mode when it isn't. This is unfortunate, but it's rooted in various flaws of ScrollView
/FlatList
's implementation and the miscellaneous workarounds we had to implement to compensate for it.
:warning: Therefore you should always check if the issue you experience also happens in a production environment. This is, sadly, the only way to test the real performance and behavior of the carousel.
For more information, you can read the following notes: "Android performance" and "Unreliable callbacks".
:warning: When debugging with the iOS simulator, you're only one "Cmd + T" away from toggling "Slow Animations". If carousel's animations seem painfully slow, make sure that you haven't enabled this setting by mistake.
FlatList
FlatList
and ScrollView
handlingFlatList
instead of ScrollView
Written by Benoît Delmaire (bd-arc) and Maxime Bertonnier (Exilz) at Archriss.
FAQs
Swiper/carousel component for React Native with previews, multiple layouts, parallax images, performant handling of huge numbers of items, and RTL support. Compatible with Android & iOS.
We found that react-native-snap-carousel demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.