Skip to content

Commit

Permalink
Fix drag drop memoized bug (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdpino authored Dec 29, 2021
1 parent d0623ae commit 53d50ee
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions src/Event/Event.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef, useEffect } from 'react';
import React, { useRef, useEffect, useMemo, useCallback } from 'react';
import PropTypes from 'prop-types';
import { Animated, PanResponder, Text, TouchableOpacity } from 'react-native';
import styles from './Event.styles';
Expand All @@ -19,27 +19,27 @@ const Event = ({
containerStyle,
onDrag,
}) => {
const isDragEnabled = () => {
return !!onDrag;
};
const isDragEnabled = !!onDrag;

const isPressDisabled = () => {
return !onPress && !onLongPress;
};
const isPressDisabled = !onPress && !onLongPress;

const onDragRelease = (dx, dy) => {
if (!onDrag) {
return;
}
const onDragRelease = useCallback(
(dx, dy) => {
if (!onDrag) {
return;
}

const newX = position.left + position.width / 2 + dx;
const newY = position.top + dy;
onDrag(event, newX, newY);
};
const newX = position.left + position.width / 2 + dx;
const newY = position.top + dy;
onDrag(event, newX, newY);
},
[event, position, onDrag],
);

const translatedByDrag = useRef(new Animated.ValueXY()).current;
const currentWidth = useRef(new Animated.Value(position.width)).current;
const currentLeft = useRef(new Animated.Value(position.left)).current;

useEffect(() => {
translatedByDrag.setValue({ x: 0, y: 0 });
const { left, width } = position;
Expand All @@ -57,15 +57,16 @@ const Event = ({
];
Animated.parallel(animations).start();
}, [position]);
const panResponder = useRef(
PanResponder.create({
onStartShouldSetPanResponder: () => isDragEnabled(),

const panResponder = useMemo(() => {
return PanResponder.create({
onStartShouldSetPanResponder: () => isDragEnabled,
onStartShouldSetPanResponderCapture: () =>
isPressDisabled() && isDragEnabled(),
isPressDisabled && isDragEnabled,
onMoveShouldSetPanResponder: (_, gestureState) =>
isDragEnabled() && hasMovedEnough(gestureState),
isDragEnabled && hasMovedEnough(gestureState),
onMoveShouldSetPanResponderCapture: (_, gestureState) =>
isPressDisabled() && isDragEnabled() && hasMovedEnough(gestureState),
isPressDisabled && isDragEnabled && hasMovedEnough(gestureState),
onPanResponderMove: Animated.event(
[
null,
Expand All @@ -86,8 +87,9 @@ const Event = ({
onPanResponderTerminate: () => {
translatedByDrag.setValue({ x: 0, y: 0 });
},
}),
).current;
});
}, [onDragRelease, isDragEnabled, isPressDisabled]);

return (
<Animated.View
style={[
Expand Down

0 comments on commit 53d50ee

Please sign in to comment.