List
Build a scrolling collection with recycled native cells, stable row identities, and targeted updates.
const renderers: ListRenderers<Rows> = { note: ({ item }) => ( <NoteRow note={item.data} /> ),}<List dataSource={notes} renderers={renderers}/>- River walkRiverside · 3.2 km
- Orchard gateOld orchard · 2.4 km
- Willow bridgeWetlands · 4.1 km
- Hilltop benchRidgeline · 5.6 km
- Lake shoreLakeside · 2.8 km
Use List for a feed, inbox, or gallery with many rows. Give the list a bounded viewport, a stable data source, and a renderer for each type of row. On iOS the host uses UICollectionView. On Android it uses RecyclerView. As you scroll, the host reuses cells and asks React to render the item now assigned to each one. For a short page with a few sections, see ScrollView.
Build a typed list
This notebook has two row types, a heading and a note. Each type selects its renderer and recycling pool. Give rows with the same layout the same type. The key identifies a particular row within that type and stays with it through edits and moves.
import { useState } from 'react'
import {
createListDataSource, List, Text, type ListRenderers,
} from 'react-nitro-renderer'
type Rows = {
heading: string
note: { title: string; saved: boolean }
}
const renderers: ListRenderers<Rows> = {
heading: ({ item }) => <Text fontWeight="bold">{item.data}</Text>,
note: ({ item }) => {
const label = item.data.saved ? `★ ${item.data.title}` : item.data.title
return <Text layout={{ padding: 12 }}>{label}</Text>
},
}
function createNotebook() {
const notes = createListDataSource<Rows>({
isContentEqualByType: {
heading: (before, after) => before.data === after.data,
note: (before, after) =>
before.data.title === after.data.title && before.data.saved === after.data.saved,
},
})
notes.replaceData([
{ type: 'heading', key: 'today', data: 'Today' },
{ type: 'note', key: 'river', data: { title: 'River walk', saved: false } },
{ type: 'note', key: 'mill', data: { title: 'Old mill', saved: true } },
])
return notes
}
export function Notebook() {
const [notes] = useState(createNotebook)
return <List dataSource={notes} renderers={renderers} layout={{ flex: 1 }} />
}useState(createNotebook) creates the data source for the lifetime of this list. Parent renders keep using the same instance. Replacing it, or changing the scroll axis, resets the scroll position and cell state. A list unsubscribes when it unmounts. If a separate owner keeps the source around, it can reuse it or call release() when finished with all attached lists.
Update the data source
Make edits on the existing source. In the example above, a bookmark belongs to the note's data. This handler creates a new payload and updates the item at its current position.
import type { ListDataSource } from 'react-nitro-renderer'
function toggleBookmark(notes: ListDataSource<Rows>, index: number) {
const item = notes.getItem(index)
if (item.type === 'note') {
const data = { ...item.data, saved: !item.data.saved }
notes.updateItem(index, { ...item, data: data })
}
}| Change | Data source call |
|---|---|
| Insert one row | insertItem(index, item) inserts before the index. Use itemCount to append. |
| Edit one row | updateItem(index, item) retains the identity when the type and key match. Changing either replaces the row. |
| Reorder or delete | moveItem(from, to) moves to the final index. removeItem(index) deletes that row. |
| Refresh, filter, or append a whole page | Build the next array and call replaceData(items) once. Pass true as the second argument to animate the differences. |
Individual insert, update, move, and remove calls request animated changes. Each call publishes a snapshot. When a network request delivers 50 rows together, one replaceData lets the native list apply a single change set. Keep stable keys from the underlying records so the host can match retained rows. Reusing an array index as the key breaks that match when an insertion shifts the rows.
Skip unnecessary row updates
On replaceData, React Nitro matches each retained row by type and key. Without a comparator it treats the supplied content as changed. isContentEqualByType lets the native host keep an unchanged binding. Return true when all fields that affect the rendered row are equal. In the notebook example, changing a bookmark must invalidate the note even when the title stays the same. A change to an item's width or height also invalidates it.
Treat item objects and their payloads as immutable. Mutating a shared object first can make the old and new snapshots both see the edited value. For a timestamp, thumbnail, or subtitle used by a row, include it in that type's comparison. Keep comparisons cheap because a full refresh can visit every retained item.
Keep state with the right row
The React subtree belongs to the recycled cell. A cell that displayed a bookmarked note can next display an unsaved one. Read bookmark, selection, and expansion state from item data or from a store keyed by the item's identity. The index passed to a renderer describes its current position and can change after a move.
For photos, give the image a bounded frame and set recyclingKey to the identity of the photo. The native image view clears its previous image when that key changes, including while the new image loads. Image
Size the viewport and rows
A vertical list fills the available width and measures each cell's height from its content. A horizontal list fills the available height and measures the cell's width. Give the parent a bounded size on both axes. If the rows have a known size, supply height or width on the item in points. Let variable-height text rows measure themselves so wrapping and text edits can change their height.
On iOS, estimatedItemSize gives the flow layout an initial estimate along the scroll axis until a cell is measured. It defaults to 44 points. Choose a positive, finite value close to a representative row. On Android, the layout manager measures its cells directly.
For a horizontal gallery, set horizontal, bound the viewport height, and give each item a width if the cards should all be the same size.
<List
horizontal
layout={{ height: 128, flex: 0 }}
estimatedItemSize={140}
dataSource={photos}
renderers={photoRenderers}
showsHorizontalScrollIndicator={false}
/>Set showsVerticalScrollIndicator or showsHorizontalScrollIndicator to control the corresponding scroll indicator. Both default to visible. contentPadding adds space inside the scrolling content. Its top, right, bottom, and left edges accept non-negative point values. Include safe-area insets there when the list extends under a device edge.
Check performance on a device
Profile a release build with representative data, row heights, and images. Try a fast scroll, a single-row edit, and a full refresh separately. If the list jumps to the start, check whether the source or axis changed. If a refresh rebinds unchanged rows, check stable keys and the per-type content comparison. If a bookmark or thumbnail appears on the wrong row after scrolling, inspect the recycled cell's state and image key.
To isolate a scrolling hitch, temporarily render a plain Text row with the same data. Compare frame times on the same device, then restore images and the rest of the row in stages. This separates work done when binding and measuring a cell from work done when applying a new data snapshot.