react-native - React-Native :使用 componentDidMount 上的 ref 计算 FlatList 的 scrollToItem 并渲染它
问题描述
我有一个 FlatList 可以呈现 1-10 的数字以及一个单选按钮。如果我预先选择了可能是动态的数字,则该编号的单选按钮是预先选择的。在这种情况下如何计算scrollToItem/scrollToIndex?
componentDidMount() {
this.scrollToItem();
}
scrollToItem = () => {
let dynamicIndex = dynamicIndex
//calcuate the dynamic Index
this.flatListRef.scrollToIndex({ animated: true, index: dynamicIndex });
};
<FlatList
ref={ref => {this.flatListRef = ref;}}
bounces={false}
keyExtractor={item => item.id}
data={listData}
renderItem={({ item, index })=>
this.renderItem(item,index)}
/>
但它会抛出错误“无法读取索引的属性'scrollToIndex'。基本上它在组件安装期间找不到'this.flatListRef'。
解决方案
推荐阅读
- python - 带有整数 xaxis 的甘特图的 Plotly Express 时间线?
- amazon-web-services - AWS: 502 BadGateway: On Launching "more like this" Ec2 实例
- postgresql - Flask-sqlalchemy 有效地获取按关系排序的查询计数
- php - 无法找到 PHP.ini 用于使用 XAMPP 为 cakePHP 激活 extension=php_intl.dll
- java - Spring @Autowired EntityManager 失败
- node.js - 有什么方法可以识别 NodeJS 中的 SQL Server 存储过程返回的错误
- flutter - Flutter:谷歌创始之选
- django - 如何在 django 3.1 中执行允许 DELETE 方法的基于类的删除视图?
- c# - dotnet-trace 不解析 linux-arm 下的符号
- linux - 如何找到使用 _do_fork() 创建新进程的基本例程?