Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Reanimated Carousel ScrollTo event not working

I am using react native reanimated carousel. In the carousel I am rendering images and when I tap on the image it doesn't scroll to that image. Here is the code:

<Carousel
        ref={carouselRef}
                loop
                width={Dimensions.get("window").width}
                height={300 / 2}
                data={carTypes}
                mode="parallax"
                defaultIndex={1}
                
                style={{ alignSelf: "center",justifyContent: "center", alignContent: "center"}}
                scrollAnimationDuration={1000}
                onSnapToItem={(index) => setActiveSlide(index)}
                renderItem={({ item, index }) => (
                    <TouchableOpacity
                    **onPress={() => carouselRef.current.scrollTo(index)}**
                    activeOpacity={0.1}
                        style={{
                            backgroundColor: activeSlide !== index ? carouselActiveSlide(index) : "#3887EF",
                            borderWidth: 1,
                            justifyContent: 'center',
                            transform: [{ rotate: "32deg" }],
                            borderRadius: 32,
                            width: 50
                        }}
                    />

I tried to test if other event like .next() , .prev() and they work as expected but .scrollTo(index) doesn't work. Can you tell me what am I missing ?

like image 832
Nika Avatar asked Feb 11 '26 12:02

Nika


1 Answers

If you look closer at the doc, you have to pass an object in scollTo with the index in it : https://github.com/dohooo/react-native-reanimated-carousel/blob/main/docs/props.md#ref

Like this :

scrollTo({index: 1})

like image 169
OneQ Avatar answered Feb 13 '26 18:02

OneQ