react-native - 大型组件作为 VirtualizedList/etc 中的部分?
问题描述
如果我想在虚拟化列表中显示一堆异构数据,默认的做法似乎是让父组件收集所有数据,以便它可以创建提供给列表组件的部分。
有没有办法避免要求父组件这样做?我想将父组件与数据收集部分分离,这样它所要做的就是声明它具有这样那样的组件,然后这些组件将负责收集数据。
如果它是 ScrollView,这将非常简单:
<ScrollView>
<SectionA>
<SectionB>
<SectionC>
</ScrollView>
但是,为了利用 VirtualizedList 的性能增益,如果每个部分都很大,我需要将每个部分的单独数据传递到 VirtualizedList。我不确定如何执行此操作或是否可能。
解决方案
不确定这是惯用的还是粗俗的 React 反模式,但我解决它的方法是将每个部分实现为纯无头数据Component
。
export type SectionDataComponentProps = {
onDataChanged?: () => void, // call this whenever the data updates.
}
export class SectionDataComponent<P : SectionDataComponentProps, S, ItemT> extends React.PureComponent<P, S> {
// Implemented by subclasses
getSectionData() : Array<SectionT<ItemT>> {
// returns an array of sections for a SectionList...
}
render() {
// business logic component only.
return null;
}
}
父组件通过使用 来跟踪它们ref
,然后根据需要调用 getSectionData()。
推荐阅读
- codeigniter-4 - 过滤器在 Codeiginter 4 中不起作用
- mongodb - 从另一个数组中删除数组元素
- elasticsearch - 简单查询字符串和多匹配查询之间的区别
- react-native-ios - 命令阶段脚本执行失败,退出代码为非零,反应原生 IOS
- c++ - 如何让我的输出文件停止打印零?
- java - java - 如何使用java中的onfinalize触发器从firebase存储中获取长期持久下载链接
- javascript - 如何在 div 中插入来自 javascript 的输出数据?
- database - SQLSTATE [23502]:非空违规:7 Laravel API 错误
- ngx-pagination - 使用 Ngx-Pagination 进行表格分页
- node.js - 使用 node.js 和 knex.js 更新 oracle db 中的多行时管理连接池问题