React Native theorytheory 0/50 · 0%
Components · easy

7. Lists with FlatList

Efficiently rendering long, scrollable data.

`FlatList` renders only the items currently visible (plus a small buffer), which is essential for a transaction history or an NFT gallery that could contain thousands of rows.

import { FlatList, Text } from "react-native";

function TxList({ txs }: { txs: { hash: string; amount: number }[] }) {
  return (
    <FlatList
      data={txs}
      keyExtractor={(tx) => tx.hash}
      renderItem={({ item }) => <Text>{item.hash}: {item.amount}</Text>}
    />
  );
}

`keyExtractor` must return a stable, unique id per item (a transaction hash is ideal); using the array index breaks state and animations when the list reorders.

Check your understanding

  1. 1. Why prefer FlatList over ScrollView for long lists?

  2. 2. What should keyExtractor return?

  3. 3. What prop supplies the array of items?