Swiper Animated
May 06, 2023
1 min
In this, we will discuss how to integrate Carousel in a React Native application. Carousels are widely used by many big websites, such as Amazon, Flipkart, etc. The same design element has been ported to mobile apps like Tinder, which uses swipe animation.
npm i react-native-snap-carousel
import React, {useRef, useState, useEffect} from 'react'; import Carousel, {ParallaxImage} from 'react-native-snap-carousel'; import { View, Text, Dimensions, StyleSheet, TouchableOpacity, Platform, } from 'react-native'; const ENTRIES1 = [ { illustration: 'https://i.imgur.com/UYiroysl.jpg', }, { illustration: 'https://i.imgur.com/UPrs1EWl.jpg', }, { illustration: 'https://i.imgur.com/MABUbpDl.jpg', }, { illustration: 'https://i.imgur.com/KZsmUi2l.jpg', }, { illustration: 'https://i.imgur.com/2nCt3Sbl.jpg', }, ]; const {width: screenWidth} = Dimensions.get('window'); const MyCarousel = props => { const [entries, setEntries] = useState([]); const carouselRef = useRef(null); useEffect(() => { setEntries(ENTRIES1); }, []); const renderItem = ({item, index}, parallaxProps) => { return ( <View style={styles.item}> <ParallaxImage source={{uri: item.illustration}} containerStyle={styles.imageContainer} style={styles.image} parallaxFactor={0.4} {...parallaxProps} /> <Text style={styles.title} numberOfLines={2}> {item.title} </Text> </View> ); }; return ( <View style={styles.container}> <Carousel ref={carouselRef} sliderWidth={screenWidth} sliderHeight={screenWidth} itemWidth={screenWidth - 60} data={entries} renderItem={renderItem} hasParallaxImages={true} /> </View> ); }; export default MyCarousel; const styles = StyleSheet.create({ container: { flex: 1, marginTop: 150, alignItems: "center", justifyContent: 'center' }, item: { width: screenWidth - 60, height: screenWidth - 60, }, imageContainer: { flex: 1, marginBottom: Platform.select({ios: 0, android: 1}), // Prevent a random Android rendering issue backgroundColor: 'white', borderRadius: 8, }, image: { ...StyleSheet.absoluteFillObject, resizeMode: 'cover', }, });
Quick Links
Legal Stuff