From 2dcc83dbf763713a1ded7c87f80fbc510767de3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ka=C4=9Fan?= Date: Thu, 19 Nov 2026 16:47:22 +0300 Subject: [PATCH 1/2] Don't call VirtualizedList's onLayout with the ListEmptyComponent's layout When the list is empty, VirtualizedList called the list's onLayout twice: once from the ScrollView with the list's own frame, and once from _onLayoutEmpty with the frame of ListEmptyComponent. Remove _onLayoutEmpty; the empty component's own onLayout is still called. --- .../Lists/VirtualizedList.js | 5 ---- .../Lists/__tests__/VirtualizedList-test.js | 29 ++++++++++++++++++- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/packages/virtualized-lists/Lists/VirtualizedList.js b/packages/virtualized-lists/Lists/VirtualizedList.js index 031267465af8..df8f78ef3510 100644 --- a/packages/virtualized-lists/Lists/VirtualizedList.js +++ b/packages/virtualized-lists/Lists/VirtualizedList.js @@ -1153,7 +1153,6 @@ class VirtualizedList extends StateSafePureComponent< return cloneElement(element, { onLayout: (event: LayoutChangeEvent) => { - this._onLayoutEmpty(event); // $FlowFixMe[prop-missing] React.Element internal inspection if (element.props.onLayout) { element.props.onLayout(event); @@ -1695,10 +1694,6 @@ class VirtualizedList extends StateSafePureComponent< this._maybeCallOnEdgeReached(); }; - _onLayoutEmpty = (e: LayoutChangeEvent) => { - this.props.onLayout && this.props.onLayout(e); - }; - _getFooterCellKey(): string { return this._getCellKey() + '-footer'; } diff --git a/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js b/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js index 5bc0254c7127..a5f785ea00de 100644 --- a/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js +++ b/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js @@ -433,10 +433,37 @@ describe('VirtualizedList', () => { // Call the onLayout handler result.props.onLayout({nativeEvent: {layout: {width: 100, height: 100}}}); - // Both the original and the new onLayout should be called + // The original onLayout should still be called expect(originalOnLayout).toHaveBeenCalled(); }); + it('does not call onLayout with the layout of ListEmptyComponent', () => { + const listRef = createRef(); + const onLayout = jest.fn(); + const element =
Empty
; + + act(() => { + create( + data[index]} + getItemCount={data => data.length} + renderItem={({item}) => } + />, + ); + }); + + const result = listRef.current._renderEmptyComponent(element, null); + result.props.onLayout({ + nativeEvent: {layout: {x: 24, y: 24, width: 338, height: 66}}, + }); + + expect(onLayout).not.toHaveBeenCalled(); + }); + it('returns the viewableItems correctly in the onViewableItemsChanged callback after changing the data', async () => { const ITEM_HEIGHT = 800; let data = [{key: 'i1'}, {key: 'i2'}, {key: 'i3'}]; From 80ae7a117e24f2414ca3dae4ea0f6b8975292db1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ka=C4=9Fan?= Date: Thu, 19 Nov 2026 18:22:48 +0300 Subject: [PATCH 2/2] Drop the ListEmptyComponent onLayout override cloneElement keeps the element's own onLayout, so the wrapper is no longer needed now that it only called that handler. --- packages/virtualized-lists/Lists/VirtualizedList.js | 6 ------ .../Lists/__tests__/VirtualizedList-test.js | 10 ++++++---- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/packages/virtualized-lists/Lists/VirtualizedList.js b/packages/virtualized-lists/Lists/VirtualizedList.js index df8f78ef3510..9d207739a3df 100644 --- a/packages/virtualized-lists/Lists/VirtualizedList.js +++ b/packages/virtualized-lists/Lists/VirtualizedList.js @@ -1152,12 +1152,6 @@ class VirtualizedList extends StateSafePureComponent< } return cloneElement(element, { - onLayout: (event: LayoutChangeEvent) => { - // $FlowFixMe[prop-missing] React.Element internal inspection - if (element.props.onLayout) { - element.props.onLayout(event); - } - }, // $FlowFixMe[prop-missing] React.Element internal inspection style: StyleSheet.compose(inversionStyle, element.props.style), }); diff --git a/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js b/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js index a5f785ea00de..0b39177a1e62 100644 --- a/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js +++ b/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js @@ -373,7 +373,7 @@ describe('VirtualizedList', () => { expect(result).toBe(fragment); }); - it('empty component clones the element and adds onLayout and style props if not a Fragment', () => { + it('empty component clones the element and adds the style prop if not a Fragment', () => { const listRef = createRef(); const element =
Test
; const inversionStyle = {transform: [{scaleY: -1}]}; @@ -396,14 +396,14 @@ describe('VirtualizedList', () => { inversionStyle, ); - // The result should be a cloned element with additional props + // The result should be a cloned element with the inversion style expect(result).not.toBe(element); expect(result.props).toEqual( expect.objectContaining({ - onLayout: expect.any(Function), style: inversionStyle, }), ); + expect(result.props.onLayout).toBeUndefined(); }); it('empty component preserves original onLayout handler if present', () => { @@ -440,7 +440,8 @@ describe('VirtualizedList', () => { it('does not call onLayout with the layout of ListEmptyComponent', () => { const listRef = createRef(); const onLayout = jest.fn(); - const element =
Empty
; + const emptyOnLayout = jest.fn(); + const element =
Empty
; act(() => { create( @@ -461,6 +462,7 @@ describe('VirtualizedList', () => { nativeEvent: {layout: {x: 24, y: 24, width: 338, height: 66}}, }); + expect(emptyOnLayout).toHaveBeenCalledTimes(1); expect(onLayout).not.toHaveBeenCalled(); });