From 1c56e92694445102e61c98e9d3e03d087e4de943 Mon Sep 17 00:00:00 2001 From: Arthur Kim <81060569+dmk3141618@users.noreply.github.com> Date: Thu, 5 Dec 2024 19:00:42 +0900 Subject: [PATCH] Update refContext.tsx [Reanimated] Tried to modify key `current` of an object which has been already passed to a worklet https://github.com/computerjazz/react-native-draggable-flatlist/issues/539 --- src/context/refContext.tsx | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/context/refContext.tsx b/src/context/refContext.tsx index ea21575c..1b7a287c 100644 --- a/src/context/refContext.tsx +++ b/src/context/refContext.tsx @@ -1,14 +1,14 @@ -import React, { useContext } from "react"; +import React, { useContext, useEffect } from "react"; import { useMemo, useRef } from "react"; import { FlatList } from "react-native-gesture-handler"; -import Animated, { WithSpringConfig } from "react-native-reanimated"; +import Animated, { type SharedValue, useSharedValue, WithSpringConfig } from "react-native-reanimated"; import { DEFAULT_PROPS } from "../constants"; import { useProps } from "./propsContext"; import { CellData, DraggableFlatListProps } from "../types"; type RefContextValue = { propsRef: React.MutableRefObject>; - animationConfigRef: React.MutableRefObject; + animationConfigRef: SharedValue; cellDataRef: React.MutableRefObject>; keyToIndexRef: React.MutableRefObject>; containerRef: React.RefObject; @@ -50,12 +50,18 @@ function useSetupRefs({ const propsRef = useRef(props); propsRef.current = props; - const animConfig = { - ...DEFAULT_PROPS.animationConfig, - ...animationConfig, - } as WithSpringConfig; - const animationConfigRef = useRef(animConfig); - animationConfigRef.current = animConfig; + const animConfig = useMemo( + () => ({ + ...DEFAULT_PROPS.animationConfig, + ...animationConfig, + } as WithSpringConfig), + [animationConfig] + ); + const animationConfigRef = useSharedValue(animConfig); + + useEffect(() => { + animationConfigRef.value = animConfig; + }, [animConfig]); const cellDataRef = useRef(new Map()); const keyToIndexRef = useRef(new Map());