React Native SectionList

17 Mar 2025 | 阅读 2 分钟

React Native 的 SectionList 组件是一个列表视图组件,它将数据列表分成逻辑部分。可以使用其 section header 属性 renderSectionHeader来实现分割数据。

要实现 SectionList 组件,我们需要从 'react-native' 库中导入 SectionList

SectionList 的属性

sectionsrenderIteminitialNumToRenderkeyExtractor
renderSectionHeaderrenderSectionFooteronRefreshinverted
extraDataonEndReachedkeyExtractorlegacyImplementation
onViewableItemsChangedrefreshingremoveClippedSubviewsListHeaderComponent
SectionSeparatorComponentstickySectionHeadersEnabledonEndReachedThresholdListEmptyComponent

React Native SectionList 示例

在这个例子中,我们创建一个带有标题和数据的 SectionList。sections 属性用于创建标题和数据值的列表。renderSectionHeader 显示列表视图的标题部分。

输出

React Native SectionList

在 SectionList 中添加分隔符

ItemSeparatorComponent 属性在数据列表之间添加分隔符。 使用此属性,调用一个renderSeparator方法,我们在其中添加一个 View 组件作为分隔符。

对 SectionList 项目执行操作

要在单击(按下)列表项时执行操作,我们使用 onPress 属性。 onPress 属性并在另一个方法 getListViewItem 中处理事件。

输出

React Native SectionList React Native SectionList