SectionList #

A performant interface for rendering sectioned lists, supporting the most handy features:

  • Fully cross-platform.
  • Configurable viewability callbacks.
  • List header support.
  • List footer support.
  • Item separator support.
  • Section header support.
  • Section separator support.
  • Heterogeneous data and item rendering support.
  • Pull to Refresh.
  • Scroll loading.

If you don't need section support and want a simpler interface, use <FlatList>.

If you need sticky section header support, use ListView for now.

Simple Examples:

<SectionList renderItem={({item}) => <ListItem title={item.title}} renderSectionHeader={({section}) => <H1 title={section.key} />} sections={[ // homogenous rendering between sections {data: [...], key: ...}, {data: [...], key: ...}, {data: [...], key: ...}, ]} /> <SectionList sections={[ // heterogeneous rendering between sections {data: [...], key: ..., renderItem: ...}, {data: [...], key: ..., renderItem: ...}, {data: [...], key: ..., renderItem: ...}, ]} />

This is a convenience wrapper around <VirtualizedList>, and thus inherits the following caveats:

  • Internal state is not preserved when content scrolls out of the render window. Make sure all your data is captured in the item data or external stores like Flux, Redux, or Relay.
  • This is a PureComponent which means that it will not re-render if props remain shallow- equal. Make sure that everything your renderItem function depends on is passed as a prop that is not === after updates, otherwise your UI may not update on changes. This includes the data prop and parent component state.
  • In order to constrain memory and enable smooth scrolling, content is rendered asynchronously offscreen. This means it's possible to scroll faster than the fill rate ands momentarily see blank content. This is a tradeoff that can be adjusted to suit the needs of each application, and we are working on improving it behind the scenes.
  • By default, the list looks for a key prop on each item and uses that for the React key. Alternatively, you can provide a custom keyExtractor prop.

Props #

ItemSeparatorComponent?: ?ReactClass<any> #

Rendered in between adjacent Items within each section.

ListFooterComponent?: ?ReactClass<any> #

Rendered at the very end of the list.

ListHeaderComponent?: ?ReactClass<any> #

Rendered at the very beginning of the list.

SectionSeparatorComponent?: ?ReactClass<any> #

Rendered in between each section.

extraData?: any #

A marker property for telling the list to re-render (since it implements PureComponent). If any of your renderItem, Header, Footer, etc. functions depend on anything outside of the data prop, stick it here and treat it immutably.

initialNumToRender: number #

How many items to render in the initial batch. This should be enough to fill the screen but not much more. Note these items will never be unmounted as part of the windowed rendering in order to improve perceived performance of scroll-to-top actions.

keyExtractor: (item: Item, index: number) => string #

Used to extract a unique key for a given item at the specified index. Key is used for caching and as the react key to track item re-ordering. The default extractor checks item.key, then falls back to using the index, like react does.

onEndReached?: ?(info: {distanceFromEnd: number}) => void #

Called once when the scroll position gets within onEndReachedThreshold of the rendered content.

onEndReachedThreshold?: ?number #

How far from the end (in units of visible length of the list) the bottom edge of the list must be from the end of the content to trigger the onEndReached callback. Thus a value of 0.5 will trigger onEndReached when the end of the content is within half the visible length of the list.

onRefresh?: ?() => void #

If provided, a standard RefreshControl will be added for "Pull to Refresh" functionality. Make sure to also set the refreshing prop correctly.

onViewableItemsChanged?: ?(info: { viewableItems: Array<ViewToken>, changed: Array<ViewToken>, }) => void #

Called when the viewability of rows changes, as defined by the viewabilityConfig prop.

refreshing?: ?boolean #

Set this true while waiting for new data from a refresh.

renderItem: (info: {item: Item, index: number}) => ?React.Element<any> #

Default renderer for every item in every section. Can be over-ridden on a per-section basis.

renderSectionHeader?: ?(info: {section: SectionT}) => ?React.Element<any> #

Rendered at the top of each section. Sticky headers are not yet supported.

sections: Array<SectionT> #

stickySectionHeadersEnabled?: boolean #

Makes section headers stick to the top of the screen until the next one pushes it off. Only enabled by default on iOS because that is the platform standard there.

You can edit the content above on GitHub and send us a pull request!

Examples #

Edit on GitHub
'use strict'; const React = require('react'); const ReactNative = require('react-native'); const { Animated, SectionList, StyleSheet, Text, View, } = ReactNative; const UIExplorerPage = require('./UIExplorerPage'); const infoLog = require('infoLog'); const { HeaderComponent, FooterComponent, ItemComponent, PlainInput, SeparatorComponent, Spindicator, genItemData, pressItem, renderSmallSwitchOption, renderStackedItem, } = require('./ListExampleShared'); const AnimatedSectionList = Animated.createAnimatedComponent(SectionList); const VIEWABILITY_CONFIG = { minimumViewTime: 3000, viewAreaCoveragePercentThreshold: 100, waitForInteraction: true, }; const renderSectionHeader = ({section}) => ( <View> <Text style={styles.headerText}>SECTION HEADER: {section.key}</Text> <SeparatorComponent /> </View> ); const CustomSeparatorComponent = ({text}) => ( <View> <SeparatorComponent /> <Text style={styles.separatorText}>{text}</Text> <SeparatorComponent /> </View> ); class SectionListExample extends React.PureComponent { static title = '<SectionList>'; static description = 'Performant, scrollable list of data.'; state = { data: genItemData(1000), debug: false, filterText: '', logViewable: false, virtualized: true, }; _scrollPos = new Animated.Value(0); _scrollSinkY = Animated.event( [{nativeEvent: { contentOffset: { y: this._scrollPos } }}], {useNativeDriver: true}, ); render() { const filterRegex = new RegExp(String(this.state.filterText), 'i'); const filter = (item) => ( filterRegex.test(item.text) || filterRegex.test(item.title) ); const filteredData = this.state.data.filter(filter); const filteredSectionData = []; let startIndex = 0; const endIndex = filteredData.length - 1; for (let ii = 10; ii <= endIndex + 10; ii += 10) { filteredSectionData.push({ key: `${filteredData[startIndex].key} - ${filteredData[Math.min(ii - 1, endIndex)].key}`, data: filteredData.slice(startIndex, ii), }); startIndex = ii; } return ( <UIExplorerPage noSpacer={true} noScroll={true}> <View style={styles.searchRow}> <PlainInput onChangeText={filterText => { this.setState(() => ({filterText})); }} placeholder="Search..." value={this.state.filterText} /> <View style={styles.optionSection}> {renderSmallSwitchOption(this, 'virtualized')} {renderSmallSwitchOption(this, 'logViewable')} {renderSmallSwitchOption(this, 'debug')} <Spindicator value={this._scrollPos} /> </View> </View> <SeparatorComponent /> <AnimatedSectionList ListHeaderComponent={HeaderComponent} ListFooterComponent={FooterComponent} SectionSeparatorComponent={() => <CustomSeparatorComponent text="SECTION SEPARATOR" /> } ItemSeparatorComponent={() => <CustomSeparatorComponent text="ITEM SEPARATOR" /> } debug={this.state.debug} enableVirtualization={this.state.virtualized} onRefresh={() => alert('onRefresh: nothing to refresh :P')} onScroll={this._scrollSinkY} onViewableItemsChanged={this._onViewableItemsChanged} refreshing={false} renderItem={this._renderItemComponent} renderSectionHeader={renderSectionHeader} sections={[ {renderItem: renderStackedItem, key: 's1', data: [ {title: 'Item In Header Section', text: 'Section s1', key: '0'}, ]}, {key: 's2', data: [ {noImage: true, title: '1st item', text: 'Section s2', key: '0'}, {noImage: true, title: '2nd item', text: 'Section s2', key: '1'}, ]}, ...filteredSectionData, ]} viewabilityConfig={VIEWABILITY_CONFIG} /> </UIExplorerPage> ); } _renderItemComponent = ({item}) => ( <ItemComponent item={item} onPress={this._pressItem} /> ); // This is called when items change viewability by scrolling into our out of // the viewable area. _onViewableItemsChanged = (info: { changed: Array<{ key: string, isViewable: boolean, item: {columns: Array<*>}, index: ?number, section?: any }>}, ) => { // Impressions can be logged here if (this.state.logViewable) { infoLog('onViewableItemsChanged: ', info.changed.map((v: Object) => ( {...v, item: '...', section: v.section.key} ))); } }; _pressItem = (index: number) => { pressItem(this, index); }; } const styles = StyleSheet.create({ headerText: { padding: 4, }, optionSection: { flexDirection: 'row', }, searchRow: { paddingHorizontal: 10, }, separatorText: { color: 'gray', alignSelf: 'center', padding: 4, fontSize: 9, }, }); module.exports = SectionListExample;