Skip to main content

VirtualizedList

React Native's list that renders only a window of cells around the viewport. A spacer takes the place of the other cells. The list reads the data through getItem and getItemCount, so the data can be any collection, not only an array.

import { Text, VirtualizedList } from 'react-native'

export function Messages({ messages }) {
return (
<VirtualizedList
data={messages}
getItemCount={(data) => data.size}
getItem={(data, index) => data.get(index)}
keyExtractor={(message) => message.id}
renderItem={({ item }) => <Text>{item.text}</Text>}
/>
)
}

The window​

The list follows the rules of React Native:

  1. The first render has the first initialNumToRender cells, or the cells from initialScrollIndex.
  2. After the first layout, the list adds maxToRenderPerBatch cells at a time, until the window covers windowSize viewports: the viewport, and half of the other viewports on each side of it.
  3. A scroll moves the window. When the viewport comes near cells that aren't rendered, the next batch renders at once. Otherwise it waits updateCellsBatchingPeriod.

Some cells stay rendered outside the window:

  • The first initialNumToRender cells, so a scroll back to the top shows them at once. With an initialScrollIndex above 0, they don't stay.
  • The closest sticky header before the window.
  • A viewport of cells on each side of the cell that has the focus, so the Tab key finds the next cell.

A cell outside the window unmounts, and its state goes with it. Keep the state in the item data or in a store.

Cell sizes​

With getItemLayout, the list knows the place of each cell at once. It measures nothing, each spacer has the exact size, and scrollToIndex reaches any cell.

Without getItemLayout, the list measures the rendered cells, and it estimates the other cells from the average length. The content then ends at the last measured cell. A scroll stops there, and the next batch moves the end further.

Server rendering

A server render has only the first initialNumToRender cells, as the first render on native. When every item must be in the HTML, for example for SEO, set initialNumToRender to the item count.

Props​

PropOn the web
data, getItem, getItemCountgetItem(data, index) and getItemCount(data) read the data. The list throws without one of them.
renderItem, ListItemComponentAs on native. With both, ListItemComponent wins, and the list logs a warning.
keyExtractorThe default uses key, then id, then the index, as on native. An item with no key or id logs a warning.
initialNumToRender, maxToRenderPerBatch, windowSize, updateCellsBatchingPeriodAs on native. The defaults are 10 cells, 10 cells, 21 viewports, and 50 ms.
getItemLayoutGives the length and offset of each cell. See Cell sizes.
initialScrollIndexThe first render starts at that cell, and the first paint shows it. Without getItemLayout, nothing is above the cell until the cells above it render.
disableVirtualizationNo spacers, and no cell unmounts. Near the end, the list adds a batch.
ItemSeparatorComponentGets highlighted and leadingItem. separators.highlight(), unhighlight(), and updateProps() in renderItem work.
ListHeaderComponent, ListFooterComponent, ListEmptyComponentA component or an element. ListHeaderComponentStyle and ListFooterComponentStyle style their wrappers. ListEmptyComponent gets the flip of inverted in its style, as on native.
horizontalA row of cells. In a right-to-left direction, the first cell is at the right edge, and offsets count from there, as on native. The list reads the direction of its scroll element.
invertedFlips the scroller and each cell with a transform, as on native, so the first cell is at the bottom.
onViewableItemsChanged, viewabilityConfig, viewabilityConfigCallbackPairsThe React Native rules. Only rendered cells count.
onEndReached, onEndReachedThresholdFires once per content length, when the window has the last cell and the scroll comes within the threshold of the end. The threshold is a share of the viewport. Without one, it's 2 px, as on native.
onStartReached, onStartReachedThresholdThe same, at the start.
onScrollToIndexFailedFires when scrollToIndex asks for a cell after the last measured cell, and there's no getItemLayout. It gets index, highestMeasuredFrameIndex, and averageItemLength.
CellRendererComponentRenders inside the cell View, with cellKey, index, item, style, and the item and separator as children.
renderScrollComponentRenders the scroller. It gets the scroll props and onRefresh, refreshing, and progressViewOffset, without the ref. The list adds no RefreshControl then, as on native.
maintainVisibleContentPositionAs on ScrollView. When items come at the start, the window moves by their count, so it keeps the same items, as on native. minIndexForVisible counts the items, not the header.
stickyHeaderIndicesAs on ScrollView. With a ListHeaderComponent, the header is index 0.
onRefresh, refreshing, progressViewOffsetWrap the scroller in a RefreshControl, as on native. With onRefresh, refreshing must be a boolean, or the list throws. A refreshControl prop wins.
extraDataA new value reports the viewable items again, as a new data does.
debugAccepted and ignored. No overlay.
ScrollView propsAs on ScrollView.

Nested lists​

A list inside a cell of a list with the same direction has no scroller of its own, as on native. It renders a View, and the outer list scrolls both:

  • The outer list sends its scroll events to the inner list, and the inner list renders the window of its items that the viewport shows.
  • The outer list renders its cells only up to the first cell whose inner list has more items, so the inner lists fill one after the other.
  • The scroll methods of an inner list, such as scrollToIndex, log a warning and do nothing, as on native. Scroll the outer list.

A list of the other direction, such as a horizontal list in a vertical list, keeps its own scroller. A list inside a Modal isn't nested in a list outside the Modal.

A list inside a plain ScrollView of the same direction logs a warning, as on native: its window can't follow the scroll. Make the outer scroller a list.

Ref​

The ref is an object with the React Native methods:

MethodOn the web
scrollToOffset({ offset, animated })As on native.
scrollToIndex({ index, viewPosition, viewOffset, animated })As on native. A fractional index scrolls into the cell. See onScrollToIndexFailed for limits.
scrollToItem({ item, viewPosition, viewOffset, animated })Finds the item through getItem and scrolls to it.
scrollToEnd({ animated })Scrolls to the end of the last cell and the footer. Without getItemLayout, the end is an estimate.
recordInteraction()Starts viewability for waitForInteraction.
hasMore()true while the window doesn't reach the last item.
getScrollRef(), getScrollResponder()The ScrollView ref.
getScrollableNode()The scroll element.
setNativeProps({ style })Writes the style on the scroll element.
flashScrollIndicators()Does nothing, as on ScrollView.