# React Native Reanimated Carousel React Native Reanimated Carousel is a high-performance carousel component for React Native applications, built on top of React Native Reanimated 2+ and React Native Gesture Handler. It provides smooth, fluid animations and supports multiple platforms including iOS, Android, and Web. The library offers infinite scrolling, customizable layouts, and various animation modes out of the box. The component is designed with performance in mind, leveraging Reanimated's worklet architecture to run animations on the native thread. It supports loop playback, auto-play functionality, gesture-based navigation, and programmatic control through imperative handles. The library includes pre-built layout modes (parallax, stack) and allows developers to create fully custom animations using worklets. ## Core Component ### Carousel Component The main carousel component that renders scrollable items with configurable animation modes and gesture handling. It uses Reanimated shared values for smooth animations and provides full control over scrolling behavior. ```tsx import React, { useRef } from 'react'; import { View, Text, Dimensions } from 'react-native'; import { useSharedValue } from 'react-native-reanimated'; import Carousel, { ICarouselInstance } from 'react-native-reanimated-carousel'; const { width } = Dimensions.get('window'); const data = [ { id: 1, color: '#B0604D', title: 'Slide 1' }, { id: 2, color: '#899F9C', title: 'Slide 2' }, { id: 3, color: '#B3C680', title: 'Slide 3' }, { id: 4, color: '#5C6265', title: 'Slide 4' }, { id: 5, color: '#F5D399', title: 'Slide 5' }, ]; function MyCarousel() { const carouselRef = useRef(null); const scrollOffsetValue = useSharedValue(0); const renderItem = ({ item, index }) => ( {item.title} ); return ( console.log('Current index:', index)} onScrollStart={() => console.log('Scroll started')} onScrollEnd={(index) => console.log('Scroll ended at:', index)} onProgressChange={(offsetProgress, absoluteProgress) => { console.log('Progress:', offsetProgress, absoluteProgress); }} renderItem={renderItem} /> ); } export default MyCarousel; ``` ## Layout Modes ### Parallax Mode Creates a parallax scrolling effect where items scale and translate with offset positioning to create depth perception during scrolling. ```tsx import React from 'react'; import { View, Dimensions } from 'react-native'; import { useSharedValue } from 'react-native-reanimated'; import Carousel from 'react-native-reanimated-carousel'; const { width } = Dimensions.get('window'); const colors = ['#B0604D', '#899F9C', '#B3C680', '#5C6265', '#F5D399', '#F1F1F1']; function ParallaxCarousel() { const progress = useSharedValue(0); return ( ( )} /> ); } export default ParallaxCarousel; ``` ### Horizontal Stack Mode Displays items in a stacked card layout where cards appear to stack behind each other with customizable spacing, rotation, and opacity effects. ```tsx import React from 'react'; import { View, Dimensions } from 'react-native'; import Carousel from 'react-native-reanimated-carousel'; const { width } = Dimensions.get('window'); const data = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FFEAA7']; function StackCarousel() { return ( ( )} /> ); } export default StackCarousel; ``` ### Vertical Stack Mode Similar to horizontal stack but items stack vertically with configurable Y-axis transformations. ```tsx import React from 'react'; import { View, Dimensions } from 'react-native'; import Carousel from 'react-native-reanimated-carousel'; const { width, height } = Dimensions.get('window'); const data = ['#E74C3C', '#3498DB', '#2ECC71', '#F39C12', '#9B59B6']; function VerticalStackCarousel() { return ( ( )} /> ); } export default VerticalStackCarousel; ``` ## Custom Animations ### Custom Animation Function Create fully customized animations using worklets with access to the animation value and item index. ```tsx import React from 'react'; import { View, Dimensions, Text } from 'react-native'; import Carousel from 'react-native-reanimated-carousel'; import { interpolate, Extrapolation } from 'react-native-reanimated'; const { width } = Dimensions.get('window'); const data = Array.from({ length: 6 }, (_, i) => ({ id: i, label: `Item ${i + 1}` })); function CustomAnimationCarousel() { const customAnimation = (value: number, index: number) => { 'worklet'; const itemSize = width * 0.8; const inputRange = [-1, 0, 1]; const translateX = interpolate( value, inputRange, [-itemSize, 0, itemSize], Extrapolation.CLAMP ); const scale = interpolate( value, inputRange, [0.8, 1, 0.8], Extrapolation.CLAMP ); const opacity = interpolate( value, inputRange, [0.5, 1, 0.5], Extrapolation.CLAMP ); const rotateY = interpolate( value, inputRange, [-45, 0, 45], Extrapolation.CLAMP ); return { transform: [ { translateX }, { scale }, { rotateY: `${rotateY}deg` }, { perspective: 1000 }, ], opacity, }; }; return ( ( {item.label} )} /> ); } export default CustomAnimationCarousel; ``` ## Imperative Controls ### Carousel Controller Methods Programmatically control carousel navigation using ref methods including next, prev, scrollTo, and getCurrentIndex. ```tsx import React, { useRef } from 'react'; import { View, Button, Text, StyleSheet } from 'react-native'; import Carousel, { ICarouselInstance } from 'react-native-reanimated-carousel'; const data = ['Red', 'Green', 'Blue', 'Yellow', 'Purple', 'Orange']; function ControlledCarousel() { const carouselRef = useRef(null); const handleNext = () => { carouselRef.current?.next({ count: 1, animated: true }); }; const handlePrev = () => { carouselRef.current?.prev({ count: 1, animated: true }); }; const handleScrollToIndex = (index: number) => { carouselRef.current?.scrollTo({ index, animated: true, onFinished: () => console.log('Scroll completed to index:', index), }); }; const handleGetCurrentIndex = () => { const currentIndex = carouselRef.current?.getCurrentIndex(); console.log('Current index:', currentIndex); }; const handleSkipAhead = () => { carouselRef.current?.scrollTo({ count: 3, animated: true, }); }; return ( ( {item} - {index} )} />