Skip to content

Commit be8524b

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
Fix invariant violation when nesting VirtualizedList inside ListEmptyComponent (#35875)
Summary: Pull Request resolved: #35875 Fixes #35871 Nested VirtualizedLists register to their parents for updates, associated to a specfific cellKey set by VirtualizedListCellContextProvider. This cellKey is usually set when rendering a cell for a data item, but we can also render a nested VirtualizedList by putting one in a ListHeaderComponent/ListFooterComponent/ListEmptyComponent. D6603342 (a010a0c) added cellKeys when we render from a header/footer, but not ListEmptyComponent, so that association would silently fail earlier. D39466677 (010da67) added extra invariants to child list handling, that are now triggered by this case, complaining because we are trying to unregister a child list we never successfully registered, due to a missing cellKey. This fixes the issue by providing a cellKey for ListEmptyComponent as well. Changelog: [General][Fixed] - Fix invariant violation when nesting VirtualizedList inside ListEmptyComponent Differential Revision: D42574462 fbshipit-source-id: 99b51f891737eeb8b3b58b94aa6d4fd04a35d12d
1 parent 473eb1d commit be8524b

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

Libraries/Lists/VirtualizedList.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -871,16 +871,19 @@ export default class VirtualizedList extends StateSafePureComponent<
871871
<ListEmptyComponent />
872872
)): any);
873873
cells.push(
874-
React.cloneElement(element, {
875-
key: '$empty',
876-
onLayout: (event: LayoutEvent) => {
877-
this._onLayoutEmpty(event);
878-
if (element.props.onLayout) {
879-
element.props.onLayout(event);
880-
}
881-
},
882-
style: StyleSheet.compose(inversionStyle, element.props.style),
883-
}),
874+
<VirtualizedListCellContextProvider
875+
cellKey={this._getCellKey() + '-empty'}
876+
key="$empty">
877+
{React.cloneElement(element, {
878+
onLayout: (event: LayoutEvent) => {
879+
this._onLayoutEmpty(event);
880+
if (element.props.onLayout) {
881+
element.props.onLayout(event);
882+
}
883+
},
884+
style: StyleSheet.compose(inversionStyle, element.props.style),
885+
})}
886+
</VirtualizedListCellContextProvider>,
884887
);
885888
}
886889

Libraries/Lists/__tests__/VirtualizedList-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,32 @@ describe('VirtualizedList', () => {
245245
expect(component).toMatchSnapshot();
246246
});
247247

248+
it('handles nested list in ListEmptyComponent', () => {
249+
const ListEmptyComponent = (
250+
<VirtualizedList {...baseItemProps(generateItems(1))} />
251+
);
252+
253+
let component;
254+
255+
ReactTestRenderer.act(() => {
256+
component = ReactTestRenderer.create(
257+
<VirtualizedList
258+
{...baseItemProps([])}
259+
ListEmptyComponent={ListEmptyComponent}
260+
/>,
261+
);
262+
});
263+
264+
ReactTestRenderer.act(() => {
265+
component.update(
266+
<VirtualizedList
267+
{...baseItemProps(generateItems(5))}
268+
ListEmptyComponent={ListEmptyComponent}
269+
/>,
270+
);
271+
});
272+
});
273+
248274
it('returns the viewableItems correctly in the onViewableItemsChanged callback after changing the data', () => {
249275
const ITEM_HEIGHT = 800;
250276
let data = [{key: 'i1'}, {key: 'i2'}, {key: 'i3'}];

0 commit comments

Comments
 (0)