Skip to content

Commit eb30a80

Browse files
okwasniewskifacebook-github-bot
authored andcommitted
fix: make sure initialScrollIndex is bigger than 0 (#36844)
Summary: Hey, `adjustCellsAroundViewport` function was checking if `props.initialScrollIndex` is truthy and -1 was returning true. This caused bugs with rendering for tvOS: react-native-tvos/react-native-tvos#485 There are warnings in the code about `initalScrollIndex` being smaller than 0 but this if statement would still allow that. ## Changelog: [General] [Fixed] - Make sure initialScrollToIndex is bigger than 0 when adjusting cells Pull Request resolved: #36844 Test Plan: Pass -1 as initialScrollToIndex. Check that this code is executed. Reviewed By: cipolleschi Differential Revision: D44856266 Pulled By: NickGerleman fbshipit-source-id: 781a1c0efeae93f00766eede4a42559dcd066d7d
1 parent 4264ddb commit eb30a80

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

packages/virtualized-lists/Lists/VirtualizedList.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ class VirtualizedList extends StateSafePureComponent<Props, State> {
616616
),
617617
};
618618
} else {
619-
// If we have a non-zero initialScrollIndex and run this before we've scrolled,
619+
// If we have a positive non-zero initialScrollIndex and run this before we've scrolled,
620620
// we'll wipe out the initialNumToRender rendered elements starting at initialScrollIndex.
621621
// So let's wait until we've scrolled the view to the right place. And until then,
622622
// we will trust the initialScrollIndex suggestion.
@@ -627,7 +627,8 @@ class VirtualizedList extends StateSafePureComponent<Props, State> {
627627
// - initialScrollIndex > 0 AND the end of the list is visible (this handles the case
628628
// where the list is shorter than the visible area)
629629
if (
630-
props.initialScrollIndex &&
630+
props.initialScrollIndex != null &&
631+
props.initialScrollIndex > 0 &&
631632
!this._scrollMetrics.offset &&
632633
Math.abs(distanceFromEnd) >= Number.EPSILON
633634
) {

packages/virtualized-lists/Lists/__tests__/__snapshots__/VirtualizedList-test.js.snap

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3122,12 +3122,53 @@ exports[`gracefully handles negative initialScrollIndex 1`] = `
31223122
/>
31233123
</View>
31243124
<View
3125-
style={
3126-
Object {
3127-
"height": 60,
3128-
}
3129-
}
3130-
/>
3125+
onFocusCapture={[Function]}
3126+
style={null}
3127+
>
3128+
<MockCellItem
3129+
value={4}
3130+
/>
3131+
</View>
3132+
<View
3133+
onFocusCapture={[Function]}
3134+
style={null}
3135+
>
3136+
<MockCellItem
3137+
value={5}
3138+
/>
3139+
</View>
3140+
<View
3141+
onFocusCapture={[Function]}
3142+
style={null}
3143+
>
3144+
<MockCellItem
3145+
value={6}
3146+
/>
3147+
</View>
3148+
<View
3149+
onFocusCapture={[Function]}
3150+
style={null}
3151+
>
3152+
<MockCellItem
3153+
value={7}
3154+
/>
3155+
</View>
3156+
<View
3157+
onFocusCapture={[Function]}
3158+
style={null}
3159+
>
3160+
<MockCellItem
3161+
value={8}
3162+
/>
3163+
</View>
3164+
<View
3165+
onFocusCapture={[Function]}
3166+
style={null}
3167+
>
3168+
<MockCellItem
3169+
value={9}
3170+
/>
3171+
</View>
31313172
</View>
31323173
</RCTScrollView>
31333174
`;

0 commit comments

Comments
 (0)