Skip to content

Commit df5c1a1

Browse files
Don't call VirtualizedList's onLayout with the ListEmptyComponent's layout
## Summary: When the list is empty, `VirtualizedList` calls the list's `onLayout` twice: once from the `ScrollView` with the list's own frame, and once from `_onLayoutEmpty` with the frame of `ListEmptyComponent`. The second call reports the empty component's position and size inside the content container, so code that sizes things from the list's width (columns, tile sizes) picks up the wrong width whenever the empty component has a margin or padding. `_onLayoutEmpty` came in with `ListEmptyComponent` in 2017 (264d67c). The header and footer handlers next to it store their own length; the empty one only forwards its event to `props.onLayout`, which looks unintended. This removes it. The empty component's own `onLayout` is still called. ## Changelog: [GENERAL] [FIXED] - `VirtualizedList` (and `FlatList`/`SectionList`) no longer calls `onLayout` with the layout of `ListEmptyComponent` ## Test Plan: - Added a test to `VirtualizedList-test.js`: the list's `onLayout` is not called when the empty component lays out. It fails on `main` (1 call, expected 0) and passes with this change. The whole file passes (89 passed, 1 skipped), and `flow full-check`, ESLint and Prettier are clean. - iOS simulator, RN 0.88.1: an empty `FlatList` with `margin: 8` and a `ListEmptyComponent` with `margin: 24`, logging every `onLayout` call: | | list `onLayout` calls | |---|---| | empty, before | `x=8 y=221 w=386 h=464`, then `x=24 y=24 w=338 h=66` | | empty, after | `x=8 y=221 w=386 h=464` only; the empty component's own `onLayout` still gets `x=24 y=24 w=338 h=66` | | one item (before and after) | `x=8 y=221 w=386 h=464` only | Differential Revision: D133240954 Pull Request resolved: #60149
1 parent 03a6765 commit df5c1a1

2 files changed

Lines changed: 33 additions & 15 deletions

File tree

‎packages/virtualized-lists/Lists/VirtualizedList.js‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,13 +1151,6 @@ class VirtualizedList extends StateSafePureComponent<
11511151
}
11521152

11531153
return cloneElement(element, {
1154-
onLayout: (event: LayoutChangeEvent) => {
1155-
this._onLayoutEmpty(event);
1156-
// $FlowFixMe[prop-missing] React.Element internal inspection
1157-
if (element.props.onLayout) {
1158-
element.props.onLayout(event);
1159-
}
1160-
},
11611154
// $FlowFixMe[prop-missing] React.Element internal inspection
11621155
style: StyleSheet.compose(inversionStyle, element.props.style),
11631156
});
@@ -1687,10 +1680,6 @@ class VirtualizedList extends StateSafePureComponent<
16871680
this._maybeCallOnEdgeReached();
16881681
};
16891682

1690-
_onLayoutEmpty = (e: LayoutChangeEvent) => {
1691-
this.props.onLayout && this.props.onLayout(e);
1692-
};
1693-
16941683
_getFooterCellKey(): string {
16951684
return this._getCellKey() + '-footer';
16961685
}

‎packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ describe('VirtualizedList', () => {
373373
expect(result).toBe(fragment);
374374
});
375375

376-
it('empty component clones the element and adds onLayout and style props if not a Fragment', () => {
376+
it('empty component clones the element and adds the style prop if not a Fragment', () => {
377377
const listRef = createRef();
378378
const element = <div>Test</div>;
379379
const inversionStyle = {transform: [{scaleY: -1}]};
@@ -396,14 +396,14 @@ describe('VirtualizedList', () => {
396396
inversionStyle,
397397
);
398398

399-
// The result should be a cloned element with additional props
399+
// The result should be a cloned element with the inversion style
400400
expect(result).not.toBe(element);
401401
expect(result.props).toEqual(
402402
expect.objectContaining({
403-
onLayout: expect.any(Function),
404403
style: inversionStyle,
405404
}),
406405
);
406+
expect(result.props.onLayout).toBeUndefined();
407407
});
408408

409409
it('empty component preserves original onLayout handler if present', () => {
@@ -433,10 +433,39 @@ describe('VirtualizedList', () => {
433433
// Call the onLayout handler
434434
result.props.onLayout({nativeEvent: {layout: {width: 100, height: 100}}});
435435

436-
// Both the original and the new onLayout should be called
436+
// The original onLayout should still be called
437437
expect(originalOnLayout).toHaveBeenCalled();
438438
});
439439

440+
it('does not call onLayout with the layout of ListEmptyComponent', () => {
441+
const listRef = createRef();
442+
const onLayout = jest.fn();
443+
const emptyOnLayout = jest.fn();
444+
const element = <div onLayout={emptyOnLayout}>Empty</div>;
445+
446+
act(() => {
447+
create(
448+
<VirtualizedList
449+
ref={listRef}
450+
data={[]}
451+
onLayout={onLayout}
452+
ListEmptyComponent={element}
453+
getItem={(data, index) => data[index]}
454+
getItemCount={data => data.length}
455+
renderItem={({item}) => <item value={item.key} />}
456+
/>,
457+
);
458+
});
459+
460+
const result = listRef.current._renderEmptyComponent(element, null);
461+
result.props.onLayout({
462+
nativeEvent: {layout: {x: 24, y: 24, width: 338, height: 66}},
463+
});
464+
465+
expect(emptyOnLayout).toHaveBeenCalledTimes(1);
466+
expect(onLayout).not.toHaveBeenCalled();
467+
});
468+
440469
it('returns the viewableItems correctly in the onViewableItemsChanged callback after changing the data', async () => {
441470
const ITEM_HEIGHT = 800;
442471
let data = [{key: 'i1'}, {key: 'i2'}, {key: 'i3'}];

0 commit comments

Comments
 (0)