FlatList
A performant interface for rendering basic, flat lists, supporting the most handy features:
- Fully cross-platform.
- Optional horizontal mode.
- Configurable viewability callbacks.
- Header support.
- Footer support.
- Separator support.
- Pull to Refresh.
- Scroll loading.
- ScrollToIndex support.
- Multiple column support.
If you need section support, use <SectionList>.
Exampleโ
- TypeScript
- JavaScript
To render multiple columns, use the numColumns prop. Using this approach instead of a flexWrap layout can prevent conflicts with the item height logic.
More complex, selectable example below.
- Passing
extraData={selectedId}toFlatListensures thatFlatListre-renders whenselectedIdchanges. BecauseFlatListimplementsPureComponent, it only re-renders if its props change. WhilerenderItemin this example is recreated on each render because it is defined inline, passingextraDatais essential wheneverrenderItemhas a stable reference (such as when memoized or defined outside the component) so thatFlatListknows that an external dependency has updated. keyExtractortells the list to use theids for the react keys instead of the defaultkeyproperty.
- TypeScript
- JavaScript
This is a convenience wrapper around <VirtualizedList>, and thus inherits its props (as well as those of <ScrollView>) that aren't explicitly listed here, along with 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
PureComponentwhich means that it will not re-render ifpropsremain shallow-equal. Make sure that everything yourrenderItemfunction depends on is passed as a prop (e.g.extraData) that is not===after updates, otherwise your UI may not update on changes. This includes thedataprop 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 and 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
keyprop on each item and uses that for the React key. Alternatively, you can provide a customkeyExtractorprop.
Reference
Propsโ
VirtualizedList Propsโ
Inherits VirtualizedList Props.
Required renderItemโ
renderItem({
item: ItemT,
index: number,
separators: {
highlight: () => void;
unhighlight: () => void;
updateProps: (select: 'leading' | 'trailing', newProps: any) => void;
}
}): JSX.Element;
Takes an item from data and renders it into the list.
Provides additional metadata like index if you need it, as well as a more generic separators.updateProps function which let you set whatever props you want to change the rendering of either the leading separator or trailing separator in case the more common highlight and unhighlight (which set the highlighted: boolean prop) are insufficient for your use case.
| Type |
|---|
| function |
item(Object): The item fromdatabeing rendered.index(number): The index corresponding to this item in thedataarray.separators(Object)highlight(Function)unhighlight(Function)updateProps(Function)select(enum('leading', 'trailing'))newProps(Object)
Example usage:
<FlatList
ItemSeparatorComponent={
Platform.OS !== 'android' &&
(({highlighted}) => (
<View
style={[style.separator, highlighted && {marginLeft: 0}]}
/>
))
}
data={[{title: 'Title Text', key: 'item1'}]}
renderItem={({item, index, separators}) => (
<TouchableHighlight
key={item.key}
onPress={() => this._onPress(item)}
onShowUnderlay={separators.highlight}
onHideUnderlay={separators.unhighlight}>
<View style={{backgroundColor: 'white'}}>
<Text>{item.title}</Text>
</View>
</TouchableHighlight>
)}
/>
Required dataโ
An array (or array-like list) of items to render. Other data types can be used by targeting VirtualizedList directly.
| Type |
|---|
| ArrayLike |
ItemSeparatorComponentโ
Rendered in between each item, but not at the top or bottom. By default, highlighted and leadingItem props are provided. renderItem provides separators.highlight/unhighlight which will update the highlighted prop, but you can also add custom props with separators.updateProps. Can be a React Component (e.g. SomeComponent), or a React element (e.g. <SomeComponent />).
| Type |
|---|
| component, function, element |
ListEmptyComponentโ
Rendered when the list is empty. Can be a React Component (e.g. SomeComponent), or a React element (e.g. <SomeComponent />).
| Type |
|---|
| component, element |
ListFooterComponentโ
Rendered at the bottom of all the items. Can be a React Component (e.g. SomeComponent), or a React element (e.g. <SomeComponent />).
| Type |
|---|
| component, element |
ListFooterComponentStyleโ
Styling for internal View for ListFooterComponent.
| Type |
|---|
| View Style |
ListHeaderComponentโ
Rendered at the top of all the items. Can be a React Component (e.g. SomeComponent), or a React element (e.g. <SomeComponent />).
| Type |
|---|
| component, element |
ListHeaderComponentStyleโ
Styling for internal View for ListHeaderComponent.
| Type |
|---|
| View Style |
ListItemComponentโ
Each data item is rendered using this element, as an alternative to renderItem. It receives the same props, and takes precedence over renderItem when both are provided.
| Type |
|---|
| component, element |
columnWrapperStyleโ
Optional custom style for multi-item rows generated when numColumns > 1.
| Type |
|---|
| View Style |
extraDataโ
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.
| Type |
|---|
| any |
getItemLayoutโ
(data, index) => {length: number, offset: number, index: number}
getItemLayout is an optional optimization that allows skipping the measurement of dynamic content if you know the size (height or width) of items ahead of time. getItemLayout is efficient if you have fixed size items, for example:
getItemLayout={(data, index) => (
{length: ITEM_HEIGHT, offset: ITEM_HEIGHT * index, index}
)}
Adding getItemLayout can be a great performance boost for lists of several hundred items. Remember to include separator length (height or width) in your offset calculation if you specify ItemSeparatorComponent.
| Type |
|---|
| function |
horizontalโ
If true, renders items next to each other horizontally instead of stacked vertically.
| Type |
|---|
| boolean |
initialNumToRenderโ
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.
| Type | Default |
|---|---|
| number | 10 |
initialScrollIndexโ
Instead of starting at the top with the first item, start at initialScrollIndex. This disables the "scroll to top" optimization that keeps the first initialNumToRender items always rendered and immediately renders the items starting at this initial index. Requires getItemLayout to be implemented.
| Type |
|---|
| number |
invertedโ
Reverses the direction of scroll. Uses scale transforms of -1.