Repository navigation
Render multi-column FlatList as a grid behind a feature flag - #59040
Open
ramya-2196 wants to merge 2 commits into
Open
ramya-2196 wants to merge 2 commits into
ramya-2196 wants to merge 2 commits into
Conversation
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
|
@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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
With
numColumns > 1,FlatListgroups items into oneVirtualizedListcell 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,FlatListinstead passes items straight through and letsVirtualizedListlay them out withexperimental_numColumns. Each item is its own cell keyed bykeyExtractor, so no item remounts when another item is added or removed, including items that move to another row, andonViewableItemsChangedkeeps reporting the right items.Existing behaviour is kept:
getItemLayout,initialScrollIndex,scrollToIndexandmaintainVisibleContentPosition.minIndexForVisiblestill take row indices;FlatListconverts them to and from the item indices of the grid.columnWrapperStyle,ItemSeparatorComponent,CellRendererComponentorstickyHeaderIndiceskeep row cells, as these depend on one cell per row. So does Windows, whoseVirtualizedListfork does not support grids.flex: 1.Changelog:
[General][Added] - Add
enableFlatListGridLayoutfeature flag to render multi-columnFlatListas a grid of item cells, so items no longer remount when other items are added or removedDifferential Revision: D124984958