reactjs - React-Native:使用 FlatList ref 的 scrollToIndex 时出错
问题描述
我正在尝试滚动到 flatlist 上的特定索引。我正在使用它的水平属性。我正在尝试使用 ref 滚动到特定索引。但我得到了错误。
scrollToIndex should be used in conjunction with getItemLayout or onScrollToIndexFailed, otherwise there is no way to know the location of offscreen indices or handle failures.
这是我的 Flatlist 代码:
<FlatList
ref={flatRef}
showsHorizontalScrollIndicator={false}
horizontal
data={data}
renderItem={renderCell}
/>
这是在特定索引上滚动的代码:
flatRef.current.scrollToIndex({
animated: false,
index: index,
viewPosition: 0.5,
});
解决方案
试试这个添加 onScrollToIndexFailed 功能
<FlatList
ref={fListRef}
onScrollToIndexFailed={info => {
const wait = new Promise(resolve => setTimeout(resolve, 700));
wait.then(() => {
fListRef.current?.scrollToIndex({ index: info.index, animated: true/false });
});
}}
/>
您也可以结帐 git repo :: https://github.com/facebook/react-native/issues/14198
推荐阅读
- ios - 如何使用 Alamofire 在 iOS13 的 GET 请求中发送 httpBody?
- windows - 如何将文件数组(3,500)从目录 A 复制到目录 B
- docker - 无法在 ubuntu 迪斯科发行版上为 docker 添加 apt-repository
- php - 有没有办法使用 collection.aggregate() 和 PHP Driver 在 MongoDB 上执行子查询?
- javascript - 从客户端向服务器端发送数据
- javascript - Javascript 获取 Array.forEach 的引用
- javascript - 使用 Jquery 拖放时自动保存数据,无需按下按钮
- reactjs - 使用 Axios 和带有 Spring Boot Security 的后端,授权令牌永远不会进入 ReactJS 请求标头
- sql - 无法在 SQL 子查询中获取 N 个不同的记录
- c# - 文本框不存储信息,如果不为空,其他人会这样做