Mobile flatlist patterns

From Intense-Visions/harness-engineering · View interactive version

Mobile flatlist patterns is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Build performant scrollable lists with FlatList, SectionList, and FlashList for large data sets

How to use this skill

Skill file

FlatList and List Patterns

Build performant scrollable lists with FlatList, SectionList, and FlashList for large data sets

When to Use

Instructions

  1. Use FlatList for simple lists, SectionList for grouped data, and FlashList for maximum performance.
import { FlatList } from 'react-native';

function OrderList({ orders }: { orders: Order[] }) {
  return (
    <FlatList
      data={orders}
      keyExtractor={(item) => item.id}
      renderItem={({ item }) => <OrderCard order={item} />}
    />
  );
}
  1. Always provide keyExtractor. Use a stable unique ID from your data. Never use array index as the key — it breaks reordering and causes incorrect recycling.
  1. Memoize renderItem components to prevent unnecessary re-renders during scrolling.
const OrderCard = memo(function OrderCard({ order }: { order: Order }) {
  return (
    <View style={styles.card}>
      <Text>{order.title}</Text>
      <Text>{order.status}</Text>
    </View>
  );
});

// Stable renderItem reference
const renderItem = useCallback(({ item }: { item: Order }) => <OrderCard order={item} />, []);
  1. Set getItemLayout when item heights are fixed. This eliminates measurement overhead and enables instant scroll-to-index.
<FlatList
  data={items}
  getItemLayout={(data, index) => ({
    length: ITEM_HEIGHT,
    offset: ITEM_HEIGHT * index,
    index,
  })}
/>
  1. Implement infinite scroll with onEndReached.
function InfiniteList() {
  const [data, setData] = useState<Item[]>([]);
  const [page, setPage] = useState(1);
  const [loading, setLoading] = useState(false);

  const loadMore = useCallback(async () => {
    if (loading) return;
    setLoading(true);
    const newItems = await fetchItems(page + 1);
    setData((prev) => [...prev, ...newItems]);
    setPage((p) => p + 1);
    setLoading(false);
  }, [page, loading]);

  return (
    <FlatList
      data={data}
      renderItem={renderItem}
      keyExtractor={(item) => item.id}
      onEndReached={loadMore}
      onEndReachedThreshold={0.5}
      ListFooterComponent={loading ? <ActivityIndicator /> : null}
    />
  );
}
  1. Add pull-to-refresh with refreshing and onRefresh.
<FlatList
  data={data}
  renderItem={renderItem}
  refreshing={isRefreshing}
  onRefresh={async () => {
    setIsRefreshing(true);
    const fresh = await fetchItems(1);
    setData(fresh);
    setIsRefreshing(false);
  }}
/>
  1. Use SectionList for grouped data with headers.
import { SectionList } from 'react-native';

const sections = [
  { title: 'Today', data: todayItems },
  { title: 'Yesterday', data: yesterdayItems },
];

<SectionList
  sections={sections}
  keyExtractor={(item) => item.id}
  renderItem={({ item }) => <ItemRow item={item} />}
  renderSectionHeader={({ section }) => <Text style={styles.header}>{section.title}</Text>}
  stickySectionHeadersEnabled
/>;
  1. Use FlashList for better performance with large lists. FlashList by Shopify uses cell recycling instead of unmounting, providing significantly better scroll performance.
npm install @shopify/flash-list
import { FlashList } from '@shopify/flash-list';

<FlashList
  data={items}
  renderItem={({ item }) => <ItemRow item={item} />}
  estimatedItemSize={80} // Required — approximate height in pixels
  keyExtractor={(item) => item.id}
/>;
  1. Handle empty states with ListEmptyComponent.
<FlatList
  data={data}
  renderItem={renderItem}
  ListEmptyComponent={<EmptyState message="No orders yet" />}
  ListHeaderComponent={<SearchBar />}
/>

Details

FlatList vs. FlashList: FlatList unmounts items as they scroll off-screen and mounts new ones. FlashList recycles views, updating existing components with new data. FlashList is typically 5-10x faster for large lists. Use FlashList when lists exceed ~100 items or when smooth 60fps scrolling is critical.

Performance tuning props:

Common mistakes:

Source

https://reactnative.dev/docs/flatlist

Process

  1. Read the instructions and examples in this document.
  2. Apply the patterns to your implementation, adapting to your specific context.
  3. Verify your implementation against the details and edge cases listed above.

Harness Integration

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.