FlatList

FlatList is an optimized list component designed for large datasets. It employs a recycler pool strategy, reusing a fixed number of DOM/native nodes to display items as they enter the viewport, ensuring responsive scrolling and stable memory usage.

Basic Usage

The component requires data, renderItem, and keyExtractor. It is recommended to provide an estimatedItemSize to assist the initial layout calculation.

import { FlatList, Text, View } from "@zynthjs/components";

const DATA = Array.from({ length: 1000 }, (_, i) => ({ id: `${i}`, title: `Item ${i}` }));

function MyList() {
  return (
    <FlatList
      data={DATA}
      renderItem={({ item }) => (
        <View style={{ padding: 16, borderBottomWidth: 1 }}>
          <Text>{item.title}</Text>
        </View>
      )}
      keyExtractor={(item) => item.id}
    />
  );
}

Advanced Features

Multi-Column Layouts

FlatList can render items in a grid by setting the numColumns prop. This is useful for photo galleries or dashboard layouts.

<FlatList
  data={photos}
  numColumns={3}
  renderItem={({ item }) => <PhotoThumbnail src={item.url} />}
  keyExtractor={(item) => item.id}
/>

Inverted Scaling and Chat Persistence

For chat interfaces, set the inverted prop to reverse the scroll direction. Use maintainVisibleContentPosition to prevent the scroll position from jumping when new items are added to the top (start) of the list.

<FlatList
  data={messages}
  inverted={true}
  maintainVisibleContentPosition={{
    minIndexForVisible: 0,
  }}
  renderItem={({ item }) => <MessageBubble message={item} />}
/>

UI-Thread Animation Syncing

To drive animations (like sticky headers) with zero bridge latency, pass a contentOffsetSharedValue. This ID updates a native SharedValue synchronously as the user scrolls.

// sharedValueId from a native animation context
<FlatList 
  data={items} 
  contentOffsetSharedValue={sharedValueId} 
/>

Recycling and Overscan

The list maintains a recycle pool (enabled by default). You can tune responsiveness using overscan:

  • Number: The number of items to pre-render beyond the viewport.
  • Object: Precise control via main (pixel distance) or multiple (viewport size multiplier).
<FlatList
  data={data}
  overscan={{ multiple: 2 }} // Pre-renders 2 viewports of content
  recycle={true}
/>

Props

PropTypeDescription
dataT[]The array of items to render.
renderItem(info) => JSX.ElementFunction to render each row.
keyExtractor(item, index) => stringReturns a unique key for each item.
numColumnsnumberNumber of columns for grid layouts.
recyclebooleanEnables node recycling (default: true).
estimatedItemSizenumberInitial guess for item height/width.
overscannumber | OverscanConfigRange of pre-rendered content.
invertedbooleanReverses the list orientation.
horizontalbooleanEnables horizontal scrolling.
onEndReached() => voidTriggered when scrolling near the end.
onEndReachedThresholdnumberThreshold (0-1) for end detection.
ListHeaderComponentJSX.ElementComponent shown at the start.
ListFooterComponentJSX.ElementComponent shown at the end.
ItemSeparatorComponentJSX.ElementComponent shown between items.
contentOffsetSharedValuenumberNative SharedValue ID for scroll tracking.

Imperative Ref

The ref provides access to the FlatListRef, which includes all ScrollView methods plus list-specific controls:

  • scrollToItem(item, animated): Scrolls to a specific data item.
  • scrollToIndex(index, animated): Scrolls to a specific numerical index.
  • scrollToOffset(offset, animated): Scrolls to a pixel position.
  • recordInteraction(): Manually tells the list to check for visibility updates.