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粉7482188462023-12-25 10:40:47
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})