Skip to content

Commit f05604c

Browse files
authored
Fix onStart callbacks of ReanimatedSwipeable being called every update (#3151)
## Description Fix `onStart` callbacks of `ReanimatedSwipeable` being called every update. Moved from #3149 ## Test plan - open `Swipeable Reanimation` example - replace the `ReanimatedSwipeable` component with the following code: ```js <ReanimatedSwipeable containerStyle={styles.swipeable} onSwipeableOpenStartDrag={(direction) => console.log(direction)} onSwipeableCloseStartDrag={(direction) => console.log(direction)}> <Text>Swipeable!</Text> </ReanimatedSwipeable> ``` - see how only one update is sent to the console, while before this PR new updates were constantly generated
1 parent 9aef009 commit f05604c

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/components/ReanimatedSwipeable.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,8 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
570570
}
571571
});
572572

573+
const dragStarted = useSharedValue<boolean>(false);
574+
573575
const panGesture = Gesture.Pan()
574576
.onUpdate((event: GestureUpdateEvent<PanGestureHandlerEventPayload>) => {
575577
userDrag.value = event.translationX;
@@ -583,18 +585,25 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
583585
? 'left'
584586
: 'right';
585587

586-
if (rowState.value === 0 && onSwipeableOpenStartDrag) {
587-
runOnJS(onSwipeableOpenStartDrag)(direction);
588-
} else if (rowState.value !== 0 && onSwipeableCloseStartDrag) {
589-
runOnJS(onSwipeableCloseStartDrag)(direction);
588+
if (!dragStarted.value) {
589+
dragStarted.value = true;
590+
if (rowState.value === 0 && onSwipeableOpenStartDrag) {
591+
runOnJS(onSwipeableOpenStartDrag)(direction);
592+
} else if (rowState.value !== 0 && onSwipeableCloseStartDrag) {
593+
runOnJS(onSwipeableCloseStartDrag)(direction);
594+
}
590595
}
596+
591597
updateAnimatedEvent();
592598
})
593599
.onEnd(
594600
(event: GestureStateChangeEvent<PanGestureHandlerEventPayload>) => {
595601
handleRelease(event);
596602
}
597-
);
603+
)
604+
.onFinalize(() => {
605+
dragStarted.value = false;
606+
});
598607

599608
if (enableTrackpadTwoFingerGesture) {
600609
panGesture.enableTrackpadTwoFingerGesture(enableTrackpadTwoFingerGesture);

0 commit comments

Comments
 (0)