首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >scrollToIndex()不是函数

scrollToIndex()不是函数
EN

Stack Overflow用户
提问于 2019-12-24 08:06:40
回答 1查看 2.3K关注 0票数 0

我目前有一个地图和一个轮播(类似于FlatList),我希望能够使用FlatListscrollToIndex方法来捕捉到数组中的某个项目。问题是,当我采用基于ScrollViewreact-native-snap-carousel库时,我失去了使用scrollToIndex方法的能力。滚动到某个索引的功能在FlatList上运行得非常好。

我使用了一个功能组件,所以我使用了useRef钩子:

代码语言:javascript
复制
    const flatListRef = useRef()
    const handleMarker = index => {
        flatListRef.current.scrollToIndex({ index, animated: true })
    }

我为我的地图使用react-native-maps,上面的handleMarker是这样的,当用户点击地图上的一个标记时,旋转木马捕捉到相关的项目:

代码语言:javascript
复制
<Marker
    key={marker.id}
    coordinate={{ latitude: marker.latitude, longitude: marker.longitude }}
    onPress={() => handleMarker(index)}
    ref={markerRef[index]}
>
</Marker>

以下是取代FlatList的旋转木马

代码语言:javascript
复制
<Carousel
    data={items}
    renderItem={({ item }) => <Item item={item}/>}
    horizontal={true}
    ref={flatListRef}
    sliderWidth={width}
    sliderHeight={100}
    itemWidth={140}
    itemHeight={100}
/>

如何在没有scrollToIndex的情况下实现这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-24 08:27:24

请尝试使用所提供的方法

代码语言:javascript
复制
const handleMarker = index => {
  flatListRef.current.snapToItem(index);    
}

参考文献:https://github.com/archriss/react-native-snap-carousel/blob/master/doc/PROPS_METHODS_AND_GETTERS.md#available-methods

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59462489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档