From e011c4c0e2f64ba014ba6b6c77e13a72f1411786 Mon Sep 17 00:00:00 2001 From: seansy Date: Mon, 21 Jan 2019 01:56:01 -0800 Subject: [PATCH] Fixes 'Invalid render range' crash (#22847) Summary: When a list is updated to have fewer items and the user immediately scrolls, the `ViewabilityHelper` `computeViewableItems` can be invoked from the scroll and cause a crash. This side effect *should* be relatively minor; the ViewabilityHelper shouldn't cause a crash, as it generally would be used for analytics to see what users are viewing. I suggest changing this to a warning instead of a crash. Other react-native developers seem to [also be getting this occasionally](https://github.com/facebook/react-native/issues/20289). Changelog: ---------- [General] [Fixed] - `Invalid render range` crash changed to warning when viewability helper detects an anomaly in list data. Pull Request resolved: https://github.com/facebook/react-native/pull/22847 Differential Revision: D13750031 Pulled By: cpojer fbshipit-source-id: 053d2baad208d1efe5b18b07ab10226032e665b8 --- Libraries/Lists/ViewabilityHelper.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Libraries/Lists/ViewabilityHelper.js b/Libraries/Lists/ViewabilityHelper.js index b40718e09..741fe56b9 100644 --- a/Libraries/Lists/ViewabilityHelper.js +++ b/Libraries/Lists/ViewabilityHelper.js @@ -121,10 +121,13 @@ class ViewabilityHelper { } let firstVisible = -1; const {first, last} = renderRange || {first: 0, last: itemCount - 1}; - invariant( - last < itemCount, - 'Invalid render range ' + JSON.stringify({renderRange, itemCount}), - ); + if (last >= itemCount) { + console.warn( + 'Invalid render range computing viewability ' + + JSON.stringify({renderRange, itemCount}), + ); + return []; + } for (let idx = first; idx <= last; idx++) { const metrics = getFrameMetrics(idx); if (!metrics) {