From 0ef770587f78389ea4c56f5ace4389d07281947c Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Wed, 7 Sep 2022 17:00:28 -0700 Subject: [PATCH] Attempt fix #2 for `cellsAroundViewport` reaching out of bounds Summary: VirtualizedList would more gracefully handle out of range cells than VirtualizedList_EXPERIMENTAL, which treats it as an invariant violation. D39244112 (https://github.com/facebook/react-native/commit/7aa203beda3cd358703c2fa535ed045771761612) attempted to fix an issue where recalculation of cells around viewport can include out of range cells, but it is still showing up later. This change adds a bounds check to the remaining branch we control, and an assertion that `computeWindowedRenderLimits` is not returing something out of range to discover if that is the cause. Reviewed By: yungsters Differential Revision: D39267445 fbshipit-source-id: 64c99da28b5b01ef61784079b586e355f73764a1 --- Libraries/Lists/VirtualizedList_EXPERIMENTAL.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Libraries/Lists/VirtualizedList_EXPERIMENTAL.js b/Libraries/Lists/VirtualizedList_EXPERIMENTAL.js index b54c238d1e8646..50b867e6cbfaa1 100644 --- a/Libraries/Lists/VirtualizedList_EXPERIMENTAL.js +++ b/Libraries/Lists/VirtualizedList_EXPERIMENTAL.js @@ -608,7 +608,9 @@ class VirtualizedList extends StateSafePureComponent { // Wait until the scroll view metrics have been set up. And until then, // we will trust the initialNumToRender suggestion if (visibleLength <= 0 || contentLength <= 0) { - return cellsAroundViewport; + return cellsAroundViewport.last >= getItemCount(data) + ? VirtualizedList._constrainToItemCount(cellsAroundViewport, props) + : cellsAroundViewport; } let newCellsAroundViewport: {first: number, last: number}; @@ -654,6 +656,10 @@ class VirtualizedList extends StateSafePureComponent { this.__getFrameMetricsApprox, this._scrollMetrics, ); + invariant( + newCellsAroundViewport.last < getItemCount(data), + 'computeWindowedRenderLimits() should return range in-bounds', + ); } if (this._nestedChildLists.size > 0) {