mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Make sure VirtualizedList's windowSize is greater than 0
Summary: Setting `windowSize = 0` doesn't make sense. Let's make sure we catch this problem in the constructor so that it doesn't cause inexplicable list behavior. Also fixed an invariant in `VirtualizeUtils` that is meant to prohibit non-monotonically-increasing offset arrays. As written, the invariant condition can never actually be violated. Reviewed By: sahrens Differential Revision: D6625302 fbshipit-source-id: b2a983cbe7bb5fbe0aed7c5d59e69a8a00672993
This commit is contained in:
committed by
Facebook Github Bot
parent
c547f783c4
commit
3559e42c55
@@ -25,17 +25,19 @@ function elementsThatOverlapOffsets(
|
||||
getFrameMetrics: (index: number) => {length: number, offset: number},
|
||||
): Array<number> {
|
||||
const out = [];
|
||||
let outLength = 0;
|
||||
for (let ii = 0; ii < itemCount; ii++) {
|
||||
const frame = getFrameMetrics(ii);
|
||||
const trailingOffset = frame.offset + frame.length;
|
||||
for (let kk = 0; kk < offsets.length; kk++) {
|
||||
if (out[kk] == null && trailingOffset >= offsets[kk]) {
|
||||
out[kk] = ii;
|
||||
outLength++;
|
||||
if (kk === offsets.length - 1) {
|
||||
invariant(
|
||||
out.length === offsets.length,
|
||||
'bad offsets input, should be in increasing order ' +
|
||||
JSON.stringify(offsets),
|
||||
outLength === offsets.length,
|
||||
'bad offsets input, should be in increasing order: %s',
|
||||
JSON.stringify(offsets),
|
||||
);
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -535,6 +535,11 @@ class VirtualizedList extends React.PureComponent<Props, State> {
|
||||
'to support native onScroll events with useNativeDriver',
|
||||
);
|
||||
|
||||
invariant(
|
||||
props.windowSize > 0,
|
||||
'VirtualizedList: The windowSize prop must be present and set to a value greater than 0.',
|
||||
);
|
||||
|
||||
this._fillRateHelper = new FillRateHelper(this._getFrameMetrics);
|
||||
this._updateCellsToRenderBatcher = new Batchinator(
|
||||
this._updateCellsToRender,
|
||||
|
||||
@@ -81,4 +81,15 @@ describe('elementsThatOverlapOffsets', function() {
|
||||
elementsThatOverlapOffsets(offsets, frames.length, ii => frames[ii]),
|
||||
).toEqual([1]);
|
||||
});
|
||||
it('errors on non-increasing offsets', function() {
|
||||
const offsets = [150, 50];
|
||||
const frames = [
|
||||
{offset: 0, length: 50},
|
||||
{offset: 50, length: 150},
|
||||
{offset: 250, length: 100},
|
||||
];
|
||||
expect(() => {
|
||||
elementsThatOverlapOffsets(offsets, frames.length, ii => frames[ii]);
|
||||
}).toThrowErrorMatchingSnapshot();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`elementsThatOverlapOffsets errors on non-increasing offsets 1`] = `"bad offsets input, should be in increasing order: [150,50]"`;
|
||||
Reference in New Issue
Block a user