Skip to content

Commit e38a361

Browse files
Tried to modify key current of an object which has been already passed to a worklet (#551)
* Apply patches from #539 * Incorporate changes from https://github.com/computerjazz/react-native-draggable-flatlist/issues/539\#issuecomment-2507728809
1 parent 3061e30 commit e38a361

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

src/components/DraggableFlatList.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ function DraggableFlatListInner<T>(props: DraggableFlatListProps<T>) {
295295
const springTo = placeholderOffset.value - activeCellOffset.value;
296296
touchTranslate.value = withSpring(
297297
springTo,
298-
animationConfigRef.current,
298+
animationConfigRef.value,
299299
() => {
300300
runOnJS(onDragEnd)({
301301
from: activeIndexAnim.value,

src/context/refContext.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import React, { useContext } from "react";
1+
import React, { useContext, useEffect } from "react";
22
import { useMemo, useRef } from "react";
33
import { FlatList } from "react-native-gesture-handler";
4-
import Animated, { WithSpringConfig } from "react-native-reanimated";
4+
import Animated, { type SharedValue, useSharedValue, WithSpringConfig } from "react-native-reanimated";
55
import { DEFAULT_PROPS } from "../constants";
66
import { useProps } from "./propsContext";
77
import { CellData, DraggableFlatListProps } from "../types";
88

99
type RefContextValue<T> = {
1010
propsRef: React.MutableRefObject<DraggableFlatListProps<T>>;
11-
animationConfigRef: React.MutableRefObject<WithSpringConfig>;
11+
animationConfigRef: SharedValue<WithSpringConfig>;
1212
cellDataRef: React.MutableRefObject<Map<string, CellData>>;
1313
keyToIndexRef: React.MutableRefObject<Map<string, number>>;
1414
containerRef: React.RefObject<Animated.View>;
@@ -50,12 +50,18 @@ function useSetupRefs<T>({
5050

5151
const propsRef = useRef(props);
5252
propsRef.current = props;
53-
const animConfig = {
54-
...DEFAULT_PROPS.animationConfig,
55-
...animationConfig,
56-
} as WithSpringConfig;
57-
const animationConfigRef = useRef(animConfig);
58-
animationConfigRef.current = animConfig;
53+
const animConfig = useMemo(
54+
() => ({
55+
...DEFAULT_PROPS.animationConfig,
56+
...animationConfig,
57+
} as WithSpringConfig),
58+
[animationConfig]
59+
);
60+
61+
const animationConfigRef = useSharedValue(animConfig);
62+
useEffect(() => {
63+
animationConfigRef.value = animConfig;
64+
}, [animConfig]);
5965

6066
const cellDataRef = useRef(new Map<string, CellData>());
6167
const keyToIndexRef = useRef(new Map<string, number>());

src/hooks/useCellTranslate.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export function useCellTranslate({ cellIndex, cellSize, cellOffset }: Params) {
101101
? activeCellSize.value * (isAfterActive ? -1 : 1)
102102
: 0;
103103

104-
return withSpring(translationAmt, animationConfigRef.current);
104+
return withSpring(translationAmt, animationConfigRef.value);
105105
}, [activeKey, cellIndex]);
106106

107107
return translate;

src/hooks/useOnCellActiveAnimation.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { useRef } from "react";
1+
import { useEffect } from "react";
22
import Animated, {
33
useDerivedValue,
4+
useSharedValue,
45
withSpring,
56
WithSpringConfig,
67
} from "react-native-reanimated";
@@ -15,8 +16,11 @@ type Params = {
1516
export function useOnCellActiveAnimation(
1617
{ animationConfig }: Params = { animationConfig: {} }
1718
) {
18-
const animationConfigRef = useRef(animationConfig);
19-
animationConfigRef.current = animationConfig;
19+
const animationConfigRef = useSharedValue(animationConfig);
20+
21+
useEffect(() => {
22+
animationConfigRef.value = animationConfig;
23+
}, [animationConfig]);
2024

2125
const isActive = useIsActive();
2226

@@ -26,8 +30,8 @@ export function useOnCellActiveAnimation(
2630
const toVal = isActive && isTouchActiveNative.value ? 1 : 0;
2731
return withSpring(toVal, {
2832
...DEFAULT_ANIMATION_CONFIG,
29-
...animationConfigRef.current,
30-
});
33+
...animationConfigRef.value,
34+
} as WithSpringConfig);
3135
}, [isActive]);
3236

3337
return {

0 commit comments

Comments
 (0)