From 7aa203beda3cd358703c2fa535ed045771761612 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Mon, 5 Sep 2022 11:45:12 -0700 Subject: [PATCH] Attempt fix 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. This attempts to fix an issue where recalculation of cells around viewport can include out of range cells. Reviewed By: rshest Differential Revision: D39244112 fbshipit-source-id: 20fe6ea552035061d9d00720fcab77b29a785771 --- Libraries/Lists/VirtualizeUtils.js | 2 +- Libraries/Lists/VirtualizedList_EXPERIMENTAL.js | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Libraries/Lists/VirtualizeUtils.js b/Libraries/Lists/VirtualizeUtils.js index 3936bf6f6491e9..3a70d9f683091e 100644 --- a/Libraries/Lists/VirtualizeUtils.js +++ b/Libraries/Lists/VirtualizeUtils.js @@ -128,7 +128,7 @@ export function computeWindowedRenderLimits( } { const itemCount = props.getItemCount(props.data); if (itemCount === 0) { - return prev; + return {first: 0, last: -1}; } const {offset, velocity, visibleLength, zoomScale = 1} = scrollMetrics; diff --git a/Libraries/Lists/VirtualizedList_EXPERIMENTAL.js b/Libraries/Lists/VirtualizedList_EXPERIMENTAL.js index 4641ec0b0860d1..ebe059a01bd14f 100644 --- a/Libraries/Lists/VirtualizedList_EXPERIMENTAL.js +++ b/Libraries/Lists/VirtualizedList_EXPERIMENTAL.js @@ -641,7 +641,9 @@ class VirtualizedList extends StateSafePureComponent { !this._scrollMetrics.offset && Math.abs(distanceFromEnd) >= Number.EPSILON ) { - return cellsAroundViewport; + return cellsAroundViewport.last >= getItemCount(data) + ? VirtualizedList._constrainToItemCount(cellsAroundViewport, props) + : cellsAroundViewport; } newCellsAroundViewport = computeWindowedRenderLimits(