Skip to content

Commit 49e2c00

Browse files
committed
🦺 remount FlatList when changing disableInfiniteScroll on the fly to avoid crash
1 parent b14daf7 commit 49e2c00

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

‎src/components/DurationScroll/index.tsx‎

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -412,12 +412,18 @@ const DurationScroll = forwardRef<DurationScrollRef, DurationScrollProps>(
412412
const [
413413
viewabilityConfigCallbackPairs,
414414
setViewabilityConfigCallbackPairs,
415-
] = useState<ViewabilityConfigCallbackPairs>([
416-
{
417-
viewabilityConfig: { viewAreaCoveragePercentThreshold: 0 },
418-
onViewableItemsChanged: onViewableItemsChanged,
419-
},
420-
]);
415+
] = useState<ViewabilityConfigCallbackPairs | undefined>(
416+
!disableInfiniteScroll
417+
? [
418+
{
419+
viewabilityConfig: {
420+
viewAreaCoveragePercentThreshold: 0,
421+
},
422+
onViewableItemsChanged: onViewableItemsChanged,
423+
},
424+
]
425+
: undefined
426+
);
421427

422428
const [flatListRenderKey, setFlatListRenderKey] = useState(0);
423429

@@ -433,13 +439,19 @@ const DurationScroll = forwardRef<DurationScrollRef, DurationScrollProps>(
433439
// if the onViewableItemsChanged callback changes, we need to update viewabilityConfigCallbackPairs
434440
// which requires the FlatList to be remounted, hence the increase of the FlatList key
435441
setFlatListRenderKey((prev) => prev + 1);
436-
setViewabilityConfigCallbackPairs([
437-
{
438-
viewabilityConfig: { viewAreaCoveragePercentThreshold: 0 },
439-
onViewableItemsChanged: onViewableItemsChanged,
440-
},
441-
]);
442-
}, [onViewableItemsChanged]);
442+
setViewabilityConfigCallbackPairs(
443+
!disableInfiniteScroll
444+
? [
445+
{
446+
viewabilityConfig: {
447+
viewAreaCoveragePercentThreshold: 0,
448+
},
449+
onViewableItemsChanged: onViewableItemsChanged,
450+
},
451+
]
452+
: undefined
453+
);
454+
}, [disableInfiniteScroll, onViewableItemsChanged]);
443455

444456
const getItemLayout = useCallback(
445457
(_: ArrayLike<string> | null | undefined, index: number) => ({
@@ -501,15 +513,13 @@ const DurationScroll = forwardRef<DurationScrollRef, DurationScrollProps>(
501513
scrollEventThrottle={16}
502514
showsVerticalScrollIndicator={false}
503515
snapToAlignment="start"
504-
// used in place of snapToOffset due to bug on Android
516+
// used in place of snapToInterval due to bug on Android
505517
snapToOffsets={[...Array(numbersForFlatList.length)].map(
506518
(_, i) => i * styles.pickerItemContainer.height
507519
)}
508520
testID="duration-scroll-flatlist"
509521
viewabilityConfigCallbackPairs={
510-
!disableInfiniteScroll
511-
? viewabilityConfigCallbackPairs
512-
: undefined
522+
viewabilityConfigCallbackPairs
513523
}
514524
windowSize={numberOfItemsToShow}
515525
/>

0 commit comments

Comments
 (0)