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
This commit is contained in:
seansy 2019-01-21 01:56:01 -08:00 committed by Facebook Github Bot
parent 9f64ba5b06
commit e011c4c0e2
1 changed files with 7 additions and 4 deletions

View File

@ -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) {