scrollToIndex() 不是函数
scrollToIndex() is not a function
我目前有一个地图和一个旋转木马(类似于FlatList
),我希望能够使用FlatList
的scrollToIndex
方法捕捉到某个项目阵列。问题是当我采用基于 ScrollView
的 react-native-snap-carousel 库时,我失去了使用 scrollToIndex
方法的能力。滚动到某个索引的功能在 FlatList
上运行得非常好。
我正在使用功能组件,所以我正在使用 useRef
挂钩:
const flatListRef = useRef()
const handleMarker = index => {
flatListRef.current.scrollToIndex({ index, animated: true })
}
我正在为我的地图使用 react-native-maps
,上面的 handleMarker
是这样的,当用户点击地图上的标记时,旋转木马会捕捉到相关项目:
<Marker
key={marker.id}
coordinate={{ latitude: marker.latitude, longitude: marker.longitude }}
onPress={() => handleMarker(index)}
ref={markerRef[index]}
>
</Marker>
以下是替换 FlatList
的轮播:
<Carousel
data={items}
renderItem={({ item }) => <Item item={item}/>}
horizontal={true}
ref={flatListRef}
sliderWidth={width}
sliderHeight={100}
itemWidth={140}
itemHeight={100}
/>
没有 scrollToIndex
我如何实现这个?
尝试改用提供的方法
const handleMarker = index => {
flatListRef.current.snapToItem(index);
}
我目前有一个地图和一个旋转木马(类似于FlatList
),我希望能够使用FlatList
的scrollToIndex
方法捕捉到某个项目阵列。问题是当我采用基于 ScrollView
的 react-native-snap-carousel 库时,我失去了使用 scrollToIndex
方法的能力。滚动到某个索引的功能在 FlatList
上运行得非常好。
我正在使用功能组件,所以我正在使用 useRef
挂钩:
const flatListRef = useRef()
const handleMarker = index => {
flatListRef.current.scrollToIndex({ index, animated: true })
}
我正在为我的地图使用 react-native-maps
,上面的 handleMarker
是这样的,当用户点击地图上的标记时,旋转木马会捕捉到相关项目:
<Marker
key={marker.id}
coordinate={{ latitude: marker.latitude, longitude: marker.longitude }}
onPress={() => handleMarker(index)}
ref={markerRef[index]}
>
</Marker>
以下是替换 FlatList
的轮播:
<Carousel
data={items}
renderItem={({ item }) => <Item item={item}/>}
horizontal={true}
ref={flatListRef}
sliderWidth={width}
sliderHeight={100}
itemWidth={140}
itemHeight={100}
/>
没有 scrollToIndex
我如何实现这个?
尝试改用提供的方法
const handleMarker = index => {
flatListRef.current.snapToItem(index);
}