Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix new swipeable using useMemo with incomplete dependency list #2937

Merged
merged 5 commits into from
Jun 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 84 additions & 62 deletions example/src/new_api/swipeable/Swipeable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import React, {
ForwardedRef,
forwardRef,
useCallback,
useEffect,
useImperativeHandle,
useMemo,
useRef,
} from 'react';
import {
Gesture,
Expand Down Expand Up @@ -216,7 +218,20 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
const showLeftProgress = useSharedValue<number>(0);
const showRightProgress = useSharedValue<number>(0);

let swipeableMethods: SwipeableMethods;
const swipeableMethods = useRef<SwipeableMethods>({
close: () => {
'worklet';
},
openLeft: () => {
'worklet';
},
openRight: () => {
'worklet';
},
reset: () => {
'worklet';
},
});

const defaultProps = {
friction: 1,
Expand All @@ -228,15 +243,15 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
overshootFriction = defaultProps.overshootFriction,
} = props;

const calculateCurrentOffset = () => {
const calculateCurrentOffset = useCallback(() => {
'worklet';
if (rowState.value === 1) {
return leftWidth.value;
} else if (rowState.value === -1) {
return -rowWidth.value - rightOffset.value;
}
return 0;
};
}, [leftWidth, rightOffset, rowState, rowWidth]);

const updateAnimatedEvent = () => {
'worklet';
Expand Down Expand Up @@ -302,50 +317,55 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
);
};

const animateRow = (
fromValue: number,
toValue: number,
velocityX?: number
) => {
'worklet';

rowState.value = Math.sign(toValue);

appliedTranslation.value = withSpring(
toValue,
{
duration: 1000,
dampingRatio: 1.2,
stiffness: 500,
velocity: velocityX,
...props.animationOptions,
},
(isFinished) => {
if (isFinished) {
if (toValue > 0 && props.onSwipeableOpen) {
runOnJS(props.onSwipeableOpen)('left', swipeableMethods);
} else if (toValue < 0 && props.onSwipeableOpen) {
runOnJS(props.onSwipeableOpen)('right', swipeableMethods);
} else if (props.onSwipeableClose) {
const closingDirection = fromValue > 0 ? 'left' : 'right';
runOnJS(props.onSwipeableClose)(
closingDirection,
swipeableMethods
);
const animateRow = useCallback(
(fromValue: number, toValue: number, velocityX?: number) => {
'worklet';

rowState.value = Math.sign(toValue);

appliedTranslation.value = withSpring(
toValue,
{
duration: 1000,
dampingRatio: 1.2,
stiffness: 500,
velocity: velocityX,
...props.animationOptions,
},
(isFinished) => {
if (isFinished) {
if (toValue > 0 && props.onSwipeableOpen) {
runOnJS(props.onSwipeableOpen)(
'left',
swipeableMethods.current
);
} else if (toValue < 0 && props.onSwipeableOpen) {
runOnJS(props.onSwipeableOpen)(
'right',
swipeableMethods.current
);
} else if (props.onSwipeableClose) {
const closingDirection = fromValue > 0 ? 'left' : 'right';
runOnJS(props.onSwipeableClose)(
closingDirection,
swipeableMethods.current
);
}
}
}
);

if (toValue > 0 && props.onSwipeableWillOpen) {
runOnJS(props.onSwipeableWillOpen)('left');
} else if (toValue < 0 && props.onSwipeableWillOpen) {
runOnJS(props.onSwipeableWillOpen)('right');
} else if (props.onSwipeableWillClose) {
const closingDirection = fromValue > 0 ? 'left' : 'right';
runOnJS(props.onSwipeableWillClose)(closingDirection);
}
);

if (toValue > 0 && props.onSwipeableWillOpen) {
runOnJS(props.onSwipeableWillOpen)('left');
} else if (toValue < 0 && props.onSwipeableWillOpen) {
runOnJS(props.onSwipeableWillOpen)('right');
} else if (props.onSwipeableWillClose) {
const closingDirection = fromValue > 0 ? 'left' : 'right';
runOnJS(props.onSwipeableWillClose)(closingDirection);
}
};
},
[appliedTranslation, props, rowState, swipeableMethods]
);

const onRowLayout = ({ nativeEvent }: LayoutChangeEvent) => {
rowWidth.value = nativeEvent.layout.width;
Expand All @@ -359,8 +379,8 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
dragOffsetFromRightEdge = 10,
} = props;

swipeableMethods = useMemo<SwipeableMethods>(
() => ({
useEffect(() => {
swipeableMethods.current = {
close() {
'worklet';
animateRow(calculateCurrentOffset(), 0);
Expand All @@ -380,18 +400,18 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
appliedTranslation.value = 0;
rowState.value = 0;
},
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[
appliedTranslation,
leftWidth,
rightOffset,
rightWidth,
rowState,
rowWidth,
userDrag,
]
);
};
}, [
animateRow,
calculateCurrentOffset,
appliedTranslation,
leftWidth,
rightOffset,
rightWidth,
rowState,
rowWidth,
userDrag,
]);

const leftAnimatedStyle = useAnimatedStyle(() => ({
transform: [
Expand All @@ -406,7 +426,7 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
{renderLeftActions(
showLeftProgress,
appliedTranslation,
swipeableMethods
swipeableMethods.current
)}
<View
onLayout={({ nativeEvent }) =>
Expand All @@ -429,7 +449,7 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
{renderRightActions(
showRightProgress,
appliedTranslation,
swipeableMethods
swipeableMethods.current
)}
<View
onLayout={({ nativeEvent }) =>
Expand Down Expand Up @@ -520,7 +540,9 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
]);
tapGesture.shouldCancelWhenOutside(true);

useImperativeHandle(ref, () => swipeableMethods, [swipeableMethods]);
useImperativeHandle(ref, () => swipeableMethods.current, [
swipeableMethods,
]);

const animatedStyle = useAnimatedStyle(() => ({
transform: [{ translateX: appliedTranslation.value }],
Expand Down
Loading