From 67eddbd6a92f4cb4f712936d9df6c15d73360f5c Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Mon, 20 Apr 2026 10:45:55 +0700 Subject: [PATCH] perf(explorer-sidebar): memoize animation context value Stabilize animateToOpen/animateToClose with useCallback and wrap the provider value in useMemo so consumers don't re-render on every parent re-render. --- .../explorer-sidebar-animation-context.tsx | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/packages/app/src/contexts/explorer-sidebar-animation-context.tsx b/packages/app/src/contexts/explorer-sidebar-animation-context.tsx index ea59bec6f..590091e70 100644 --- a/packages/app/src/contexts/explorer-sidebar-animation-context.tsx +++ b/packages/app/src/contexts/explorer-sidebar-animation-context.tsx @@ -1,4 +1,12 @@ -import { createContext, useContext, useEffect, useRef, type ReactNode } from "react"; +import { + createContext, + useCallback, + useContext, + useEffect, + useMemo, + useRef, + type ReactNode, +} from "react"; import { useWindowDimensions } from "react-native"; import { useSharedValue, withTiming, Easing, type SharedValue } from "react-native-reanimated"; import { type GestureType } from "react-native-gesture-handler"; @@ -93,7 +101,7 @@ export function ExplorerSidebarAnimationProvider({ children }: { children: React backdropOpacity.value = targets.backdropOpacity; }, [isOpen, translateX, backdropOpacity, windowWidth, isGesturing]); - const animateToOpen = () => { + const animateToOpen = useCallback(() => { "worklet"; translateX.value = withTiming(0, { duration: ANIMATION_DURATION, @@ -103,9 +111,9 @@ export function ExplorerSidebarAnimationProvider({ children }: { children: React duration: ANIMATION_DURATION, easing: ANIMATION_EASING, }); - }; + }, [translateX, backdropOpacity]); - const animateToClose = () => { + const animateToClose = useCallback(() => { "worklet"; translateX.value = withTiming(windowWidth, { duration: ANIMATION_DURATION, @@ -115,22 +123,25 @@ export function ExplorerSidebarAnimationProvider({ children }: { children: React duration: ANIMATION_DURATION, easing: ANIMATION_EASING, }); - }; + }, [translateX, backdropOpacity, windowWidth]); + + const value = useMemo( + () => ({ + translateX, + backdropOpacity, + windowWidth, + animateToOpen, + animateToClose, + isGesturing, + gestureAnimatingRef, + openGestureRef, + closeGestureRef, + }), + [translateX, backdropOpacity, windowWidth, animateToOpen, animateToClose, isGesturing], + ); return ( - + {children} );