1- import React , { forwardRef } from "react" ;
1+ import React , { forwardRef , useMemo } from "react" ;
22import {
33 Gesture ,
44 GestureDetector ,
88 interpolate ,
99 interpolateColor ,
1010 runOnJS ,
11+ useAnimatedReaction ,
1112 useAnimatedStyle ,
1213 useSharedValue ,
1314 withSpring ,
@@ -21,6 +22,7 @@ import { createComponent } from "../../utils/createComponent";
2122
2223export type SwitchSize = "sm" | "md" | "lg" | "xl" ;
2324export type SwitchTheme = "base" | "primary" ;
25+
2426export interface SwitchProps extends BoxProps {
2527 /**
2628 * Default Value of the switch
@@ -126,6 +128,7 @@ const RNSwitch: React.FC<Partial<SwitchProps>> = forwardRef<
126128 onChange : onStateChange ,
127129 } ) ;
128130
131+ const thumbAnimated = useSharedValue ( switchState ? 1 : 0 ) ;
129132 /**
130133 * Setting Active/Inactive and Default Colors
131134 */
@@ -166,15 +169,34 @@ const RNSwitch: React.FC<Partial<SwitchProps>> = forwardRef<
166169 /**
167170 * The Switch Animation Helpers
168171 */
169- const interpolatedWidths = switchTheme . size [ size ] ?. switchInterpolateWidths ;
172+ const interpolatedWidths = useMemo ( ( ) => {
173+ return switchTheme . size [ size ] ?. switchInterpolateWidths ;
174+ // eslint-disable-next-line react-hooks/exhaustive-deps
175+ } , [ size ] ) ;
170176
171- const translatedThumbDistance = switchTheme . size [ size ] ?. thumbTranslateValue ;
172- const initTranslatedThumbDistance =
173- switchTheme . size [ size ] ?. thumbInitTranslateValue ;
174- const intermediateThumbTranslateValue =
175- switchTheme . size [ size ] ?. thumbIntermediateTranslateValue ;
177+ const translatedThumbDistance = useMemo ( ( ) => {
178+ return switchTheme . size [ size ] ?. thumbTranslateValue ;
179+ // eslint-disable-next-line react-hooks/exhaustive-deps
180+ } , [ size ] ) ;
181+ const initTranslatedThumbDistance = useMemo ( ( ) => {
182+ return switchTheme . size [ size ] ?. thumbInitTranslateValue ;
183+ // eslint-disable-next-line react-hooks/exhaustive-deps
184+ } , [ size ] ) ;
185+ const intermediateThumbTranslateValue = useMemo ( ( ) => {
186+ return switchTheme . size [ size ] ?. thumbIntermediateTranslateValue ;
187+ // eslint-disable-next-line react-hooks/exhaustive-deps
188+ } , [ size ] ) ;
176189
177- const thumbAnimated = useSharedValue ( switchState ? 1 : 0 ) ;
190+ useAnimatedReaction (
191+ ( ) => switchState ,
192+ ( ) => {
193+ if ( switchState ) {
194+ thumbAnimated . value = withSpring ( 1 , SPRING_CONFIG ) ;
195+ } else {
196+ thumbAnimated . value = withSpring ( 0 , SPRING_CONFIG ) ;
197+ }
198+ } ,
199+ ) ;
178200
179201 const animatedSwitchBackground = useAnimatedStyle ( ( ) => {
180202 return {
0 commit comments