Command palette

@yasinsensoy/native

React Native · Components

VirtualList

A virtual list for very long lists: only the visible items (and a bit more) are rendered.

3 exports

Live example

The preview runs in the browser with the web component; the native component takes the same props and looks the same on iOS and Android.

Preview

Person 1
Long note: this row is taller than the others because it has a description that wraps onto two lines.
Person 2
Person 3
Person 4
Person 5
Person 6
Person 7
Person 8
Long note: this row is taller than the others because it has a description that wraps onto two lines.
Person 9
Person 10
Person 11
Person 12
Person 13
Person 14
Person 15
Long note: this row is taller than the others because it has a description that wraps onto two lines.
Person 16
Person 17
Person 18
Person 19

Code

import { VirtualList, PersonRow } from "@yasinsensoy/native";

<VirtualList items={contacts} getKey={(k) => k.id} estimateSize={44}
  className="h-96 rounded-lg border" aria-label="People"
  renderItem={(p) => <PersonRow person={p} />} />

Events

  1. Events appear here when you interact with the preview.

Import

import { VirtualList } from "@yasinsensoy/native";

VirtualList

component

A virtual list for very long lists: only the visible items (and a bit more) are rendered. Wraps React Native's FlatList with the library's rules: infinite loading (onLoadMore, Promise awaited, retry on error), empty state, pull to refresh and measurement-free layout with a fixed height. Same prop names as the web VirtualList.

VirtualList({ ref, items, renderItem, getKey, estimateSize, overscan, gap, horizontal, height, empty, onLoadMore, onError, hasMore, loading, threshold, header, onRefresh, style, contentStyle, }: VirtualListProps<T>): JSX.Element

Examples

<VirtualList items={products} estimateSize={72} getKey={(p) => p.id}
  renderItem={(p) => <ProductRow product={p} />}
  onLoadMore={nextPage} hasMore={hasMore} empty={<EmptyState />} />

Methods of the VirtualList ref.

NameTypeDescription
scrollToIndex(index: number, options?: { align?: "start" | "center" | "end"; smooth?: boolean; }) => voidScrolls to an item. align: the item comes to the start, center or end.
scrollToTop(options?: { smooth?: boolean; }) => voidScrolls to the very top.

VirtualList props.

NameTypeDefaultDescription
ref?Ref<VirtualListHandle>Ref: scrollToIndex, scrollToTop.
itemsreadonly T[]Items.
renderItem(item: T, index: number) => ReactNodeRenders an item.
getKey?(item: T, index: number) => string | numberits indexThe item's stable key.
estimateSize?numberItem height (width when horizontal). With a fixed number the list lays out without measuring and jumps to a distant item instantly (the fastest path).
overscan?number5How many times the visible area is rendered ahead.
gap?number0Gap between items (pixels).
horizontal?booleanHorizontal list.
height?numberList height; fills the container if omitted (flex: 1).
empty?ReactNodeContent shown when the list is empty (e.g. <EmptyState />).
onLoadMore?() => unknownCalled when nearing the end (infinite list). If it returns a Promise it is not called again until it settles; on error "Try again" appears at the bottom.
onError?(error: unknown) => voidWhen onLoadMore fails.
hasMore?booleantrueWhether there are more items; onLoadMore is not called when false.
loading?booleanLoading state from outside (indicator at the bottom).
threshold?number600How many pixels before the end loading starts.
header?ReactNodeContent at the start of the list.
onRefresh?() => unknownPull to refresh: if it returns a Promise the indicator spins until it settles.
style?StyleProp<ViewStyle>List style.
contentStyle?StyleProp<ViewStyle>Style of the content container (for padding).