开发者社区> 问答> 正文

scroll ToIndex()不是函数

我现在有一张地图和一个旋转木马(类似于FlatList)我想要能够使用scrollToIndex方法FlatList切换到数组中的某一项。问题是当我采纳了反应-本地-快速-旋转木马库,它的基础是ScrollView,我失去了使用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?

展开
收起
游客5akardh5cojhg 2019-12-24 13:05:26 2121 0
1 条回答
写回答
取消 提交回答
  • 尝试使用提供的方法代替

    const handleMarker = index => {
      flatListRef.current.snapToItem(index);    
    }
    
    2019-12-24 13:05:45
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载