Every batch FlatList renders costs the sum of its rows: measuring them, rendering them, and rendering them again when the screen around the list updates. Cutting that per-row cost makes each batch shorter, which helps fill rate and responsiveness together instead of trading one for the other.

Key terms

  • getItemLayout: an optional FlatList prop, (data, index) => {length, offset, index}, that tells the list where an item sits without measuring it.
  • ItemSeparatorComponent: a component FlatList renders between items, but not above the first item or below the last.
  • PureComponent: a class component that does not re-render while its props stay shallow-equal. FlatList is one.
  • memo: the React function that creates a component which re-renders only when its props change.

Rows the list never has to measure

Without getItemLayout, the list learns each row's size by measuring it after it renders, and estimates the position of unmeasured rows from the average length of the rows measured so far. When every row has the same height (or width, for a horizontal list), getItemLayout removes the need to manage those async layout calculations: cell metrics come straight from the function. On lists of several hundred items, that can speed things up a lot.

The function returns three values for an index. length is the row's size along the scroll axis, index is the index passed in, and offset is the distance from the start of the list to the start of that row. For fixed rows, offset is the row size times the index.

A separator changes the offset. If you pass ItemSeparatorComponent, its length has to be part of the offset calculation. Separators sit between items only, so the row at index i has i rows and i separators before it. With 72 point rows and a 1 point separator, the row at index 3 starts at (72 + 1) * 3, which is 219. Leave the separator out and every computed offset drifts one more point per row.

getItemLayout also matters for scrolling by index. scrollToIndex cannot reach a location outside the render window without it, and calling it past the measured rows with neither getItemLayout nor onScrollToIndexFailed throws an invariant error.

It only fits rows whose size you know ahead of time. If rows have dynamic size and performance really matters, consider whether the design can change.

Re-renders the row can skip

Wrapping the row component in memo makes it re-render only when its props change. You can pass a comparison function as the second argument, and when it returns true, the row does not re-render.

renderItem deserves the same care. In a function component, move it out of the returned JSX and wrap it in useCallback, so it is not recreated on every render. In a class component, define it outside render.

A stable renderItem has a side effect. FlatList is a PureComponent, so it does not re-render while its props stay shallow-equal. Once renderItem keeps the same reference, the list has no way to see that something it reads from outside data changed. Anything like that, such as a selected id, has to go in extraData, or the rows will not update.

Rows with less in them

The more complex a row is, the slower it renders, so keep logic and nesting out of list items. If a row component is reused across the app, build a separate one for the big lists with as little logic and nesting as possible. Use as few effects, interactions and details per row as you can, and leave the rest for the item's detail screen.

Images deserve their own check. Every image in a list is a new Image() instance, and the sooner it reaches its loaded hook, the sooner the JavaScript thread is free again. Use a cropped version or a thumbnail, as small as possible, instead of the full-size asset.

An episode list built from cheap rows

The EpisodeList example combines the patterns. EpisodeRow is wrapped in memo and shows a thumbnail with two lines of text. renderItem is created once with useCallback and an empty dependency array, which is safe because it reads nothing outside the item.

The layout function lives at module level, since it depends only on constants:

offset: (ROW_HEIGHT + SEPARATOR_HEIGHT) * index

That line includes the separator height because the list passes ItemSeparatorComponent={Separator}, and length stays the row height. If the row later shows a selected state read from screen state, that value goes into extraData too.

Code Examples

tsx
import {memo, useCallback} from 'react';
import {FlatList, Image, StyleSheet, Text, View} from 'react-native';
import type {ListRenderItem} from 'react-native';

type Episode = {
  id: string;
  title: string;
  duration: string;
  thumbnailUrl: string; // small cropped image, not the full-size cover
};

const ROW_HEIGHT = 72;
const SEPARATOR_HEIGHT = 1;

const EpisodeRow = memo(function EpisodeRow({episode}: {episode: Episode}) {
  return (
    <View style={styles.row}>
      <Image source={{uri: episode.thumbnailUrl}} style={styles.thumbnail} />
      <View>
        <Text>{episode.title}</Text>
        <Text>{episode.duration}</Text>
      </View>
    </View>
  );
});

function Separator() {
  return <View style={styles.separator} />;
}

function getItemLayout(_data: unknown, index: number) {
  return {
    length: ROW_HEIGHT,
    offset: (ROW_HEIGHT + SEPARATOR_HEIGHT) * index,
    index,
  };
}

export function EpisodeList({episodes}: {episodes: Episode[]}) {
  const renderItem = useCallback<ListRenderItem<Episode>>(
    ({item}) => <EpisodeRow episode={item} />,
    [],
  );

  return (
    <FlatList
      data={episodes}
      keyExtractor={item => item.id}
      renderItem={renderItem}
      ItemSeparatorComponent={Separator}
      getItemLayout={getItemLayout}
    />
  );
}

const styles = StyleSheet.create({
  row: {height: ROW_HEIGHT, flexDirection: 'row', alignItems: 'center'},
  thumbnail: {width: 56, height: 56, marginHorizontal: 8},
  separator: {height: SEPARATOR_HEIGHT, backgroundColor: '#e5e5e5'},
});
✓ Completed