Repository navigation
Fix FlatList scrollToItem doing nothing when numColumns > 1 - #59191
dawidmatyjasik wants to merge 1 commit into
Conversation
|
Hi @dawidmatyjasik! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
There was a problem hiding this comment.
🟢 Approval recommended
The focused fix follows existing row-index semantics, includes regression coverage, and has no identified blocking issues.
No open issues 🎉
What changed in this PR
Fixes FlatList.scrollToItem() for multi-column lists by scrolling to the row containing the requested item.
Changes:
- Converts the matching item’s index to a row index, preserving single-column behavior.
- Adds a Fantom regression test verifying the expected scroll offset.
| File | Description |
|---|---|
| packages/react-native/Libraries/Lists/FlatList.js | Handles multi-column item lookup and row scrolling. |
| packages/react-native/Libraries/Lists/__tests__/FlatList-itest.js | Tests scrolling to an item in a two-column list. |
Review effort: Balanced
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
8953dd9 to
24c3139
Compare
Summary:
FlatList.scrollToItem()does nothing whennumColumnsis greater than 1. No error or warning is shown, and the list does not scroll.With
numColumns > 1,FlatListgivesVirtualizedListone row per index: itsgetItemreturns a new array of up tonumColumnsitems, for example[item4, item5].FlatList.scrollToItemforwarded the call toVirtualizedList.scrollToItem, which finds the item with:A row array is never
===to a single item, so the item is never found and nothing happens. With one column this works, becausegetItemreturns the item itself.This change makes
FlatList.scrollToItemfind the item indatawhennumColumns > 1, and callscrollToIndexwith the index of the row that holds it (Math.floor(index / numColumns)).scrollToIndexandgetItemLayoutalready take row indices for multi-column lists, so this matches the existing behaviour. Single-column lists still go throughVirtualizedList.scrollToItem, as before. If the item is not indata, nothing happens, like before.Changelog:
[GENERAL] [FIXED] - Fix
FlatList.scrollToItemdoing nothing whennumColumnsis greater than 1Test Plan:
Added a Fantom test next to the existing
scrollToItemtests inFlatList-itest.js: a 2-column list with rows of 50, callingscrollToItemwithDATA[5](row 2) should dispatchscrollToto offset 100.Without the fix:
With the fix:
yarn flow-check, ESLint and Prettier pass on the changed files.I also checked it in RNTester on an iOS 27 simulator (iPhone 17), with this Playground: a
FlatListwith 60 items,numColumns={2},getItemLayout, and a button that callsscrollToItem({item: DATA[30], animated: true}). Before the fix, pressing the button does nothing. After the fix, the list scrolls so that item 30 (orange) is at the top.RNTesterPlayground.js
flatlist-scrollToItem-before.mp4
flatlist-scrollToItem-after.mp4