不确定这是否是惯用的或严重的反模式,但我解决它的方法是将每个部分实现为纯无头数据。
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()。