From 33b827c613bd9609076393ceede98ef9ffc65aee Mon Sep 17 00:00:00 2001 From: Islam Rustamov Date: Sat, 7 Dec 2024 14:40:27 +0300 Subject: [PATCH] fix: handle unnecessary invocation of handleSnapToIndex on onAnimate recreation --- src/components/bottomSheet/BottomSheet.tsx | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/components/bottomSheet/BottomSheet.tsx b/src/components/bottomSheet/BottomSheet.tsx index c79181a5..7f55f478 100644 --- a/src/components/bottomSheet/BottomSheet.tsx +++ b/src/components/bottomSheet/BottomSheet.tsx @@ -6,6 +6,7 @@ import React, { useImperativeHandle, memo, useEffect, + useRef, } from 'react'; import { type Insets, Platform } from 'react-native'; import { State } from 'react-native-gesture-handler'; @@ -1854,14 +1855,21 @@ const BottomSheetComponent = forwardRef( [reduceMotion, handleOnChange, _providedOnClose] ); + /** + * Remember previously provided index, so that we don't cause + * unnecessary handleSnapToIndex + */ + const previousProvidedIndex = useRef(_providedIndex) + /** * React to `index` prop to snap the sheet to the new position. * * @alias onIndexChange */ useEffect(() => { - if (isAnimatedOnMount.value) { + if (isAnimatedOnMount.value && previousProvidedIndex.current !== _providedIndex) { handleSnapToIndex(_providedIndex); + previousProvidedIndex.current = _providedIndex } }, [_providedIndex, isAnimatedOnMount, handleSnapToIndex]); //#endregion