React Native Reanimated Carousel ScrollTo event not working
P粉232793765
P粉232793765 2023-12-24 15:44:11
0
1
589

I am using react native to revive carousel. In a carousel I am rendering an image and when I click on the image it does not scroll to the image. This 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 testing other events like .next() , .prev() and they work as expected but .scrollTo(index) doesn't work. Can you tell me what I'm missing?

P粉232793765
P粉232793765

reply all(1)
P粉748218846

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

like this:

scrollTo({index: 1})

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template