Skip to content

Render multi-column FlatList as a grid behind a feature flag - #59040

Open
ramya-2196 wants to merge 2 commits into
react:mainfrom
ramya-2196:export-D124984958
Open

ramya-2196 wants to merge 2 commits into
react:mainfrom
ramya-2196:export-D124984958

Conversation

@ramya-2196

Copy link
Copy Markdown
Contributor

Summary:
With numColumns > 1, FlatList groups items into one VirtualizedList cell per row and keys each row by the keys of its items. Adding or removing an item moves every later item into a different row, so React remounts the items of every later row and they lose their state (#45895).

Behind the new JS-only feature flag enableFlatListGridLayout, FlatList instead passes items straight through and lets VirtualizedList lay them out with experimental_numColumns. Each item is its own cell keyed by keyExtractor, so no item remounts when another item is added or removed, including items that move to another row, and onViewableItemsChanged keeps reporting the right items.

Existing behaviour is kept:

  • getItemLayout, initialScrollIndex, scrollToIndex and maintainVisibleContentPosition.minIndexForVisible still take row indices; FlatList converts them to and from the item indices of the grid.
  • Lists that use columnWrapperStyle, ItemSeparatorComponent, CellRendererComponent or stickyHeaderIndices keep row cells, as these depend on one cell per row. So does Windows, whose VirtualizedList fork does not support grids.
  • One visible difference: an item alone in a partially filled last row keeps its column width instead of being stretched across the whole row by flex: 1.

Changelog:
[General][Added] - Add enableFlatListGridLayout feature flag to render multi-column FlatList as a grid of item cells, so items no longer remount when other items are added or removed

Differential Revision: D124984958

Summary:

`FlatList` builds multi-column lists by grouping items into one row cell per row and keying each row by the keys of its items. Adding or removing an item shifts every later item into a different row, so every later row gets a new key and React remounts all of its items: they lose their state and replay their entry animations (react#45895).

This adds `experimental_numColumns` to `VirtualizedList` so the list can lay out the grid itself instead. Every item is rendered as its own cell, a direct child of the list keyed by `keyExtractor`, so an item keeps its identity when it moves to another row:

- The content container wraps cells into rows (`flexDirection: 'row'`, `flexWrap: 'wrap'`). Each cell is `100% / numColumns` wide and uses a row direction, so an item with `flex: 1` fills its column the way it fills its share of a `FlatList` row today. The header, footer, empty component and spacers are full width.
- Render windows are widened to whole rows (`expandCellsToWholeRows`) wherever the render mask is built, so rendered cells and spacers always cover complete rows.
- `ListMetricsAggregator` estimates unmeasured cells by row, since cells in a row share their row's offset and length.
- Viewability is reported per item without any change, because every item is its own cell.
- `getItemLayout` receives item indices and returns the offset and length of the item's row.
- On Android, inverted lists are flipped on both axes, so rows are reversed in the content container to keep items in reading order.

Horizontal lists, `ItemSeparatorComponent` and `stickyHeaderIndices` are not supported with `experimental_numColumns` yet and throw. `FlatList` does not use the prop yet; that follows in the next diff behind a feature flag.

Changelog:
[General][Added] - Add experimental `experimental_numColumns` prop to `VirtualizedList` to lay out items in a grid with one cell per item

Differential Revision: D124984957
Summary:
With `numColumns > 1`, `FlatList` groups items into one `VirtualizedList` cell per row and keys each row by the keys of its items. Adding or removing an item moves every later item into a different row, so React remounts the items of every later row and they lose their state (react#45895).

Behind the new JS-only feature flag `enableFlatListGridLayout`, `FlatList` instead passes items straight through and lets `VirtualizedList` lay them out with `experimental_numColumns`. Each item is its own cell keyed by `keyExtractor`, so no item remounts when another item is added or removed, including items that move to another row, and `onViewableItemsChanged` keeps reporting the right items.

Existing behaviour is kept:
- `getItemLayout`, `initialScrollIndex`, `scrollToIndex` and `maintainVisibleContentPosition.minIndexForVisible` still take row indices; `FlatList` converts them to and from the item indices of the grid.
- Lists that use `columnWrapperStyle`, `ItemSeparatorComponent`, `CellRendererComponent` or `stickyHeaderIndices` keep row cells, as these depend on one cell per row. So does Windows, whose `VirtualizedList` fork does not support grids.
- One visible difference: an item alone in a partially filled last row keeps its column width instead of being stretched across the whole row by `flex: 1`.

Changelog:
[General][Added] - Add `enableFlatListGridLayout` feature flag to render multi-column `FlatList` as a grid of item cells, so items no longer remount when other items are added or removed

Differential Revision: D124984958
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 13, 2026
@meta-codesync

meta-codesync Bot commented Oct 13, 2026

Copy link
Copy Markdown

@ramya-2196 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D124984958.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. meta-exported p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant