1
1
import _ from 'lodash' ;
2
- import React , { ReactElement , useImperativeHandle , useCallback , useMemo , useEffect } from 'react' ;
2
+ import React , { ReactElement , useImperativeHandle , useCallback , useMemo , useEffect , useRef } from 'react' ;
3
3
import { StyleSheet , AccessibilityRole , StyleProp , ViewStyle , GestureResponderEvent , LayoutChangeEvent , ViewProps , AccessibilityProps } from 'react-native' ;
4
4
import { useSharedValue , useAnimatedStyle , runOnJS , useAnimatedReaction , withTiming } from 'react-native-reanimated' ;
5
5
import { forwardRef , ForwardRefInjectedProps , Constants } from '../../commons/new' ;
@@ -222,6 +222,8 @@ const Slider = React.memo((props: Props) => {
222
222
const end = useSharedValue ( 0 ) ;
223
223
const defaultThumbOffset = useSharedValue ( 0 ) ;
224
224
const rangeThumbOffset = useSharedValue ( 0 ) ;
225
+
226
+ const didValueUpdate = useRef ( false ) ;
225
227
226
228
const thumbBackground : StyleProp < ViewStyle > = useMemo ( ( ) => [
227
229
{ backgroundColor : disabled ? disabledThumbTintColor : thumbTintColor }
@@ -235,37 +237,45 @@ const Slider = React.memo((props: Props) => {
235
237
const _thumbStyle = useSharedValue ( StyleUtils . unpackStyle ( customThumbStyle || defaultThumbStyle , { flatten : true } ) ) ;
236
238
const _activeThumbStyle = useSharedValue ( StyleUtils . unpackStyle ( activeThumbStyle , { flatten : true } ) ) ;
237
239
238
- useEffect ( ( ) => {
239
- if ( ! thumbStyle ) {
240
- _thumbStyle . value = StyleUtils . unpackStyle ( defaultThumbStyle , { flatten : true } ) ;
241
- }
242
- // eslint-disable-next-line react-hooks/exhaustive-deps
243
- } , [ defaultThumbStyle , thumbStyle ] ) ;
244
-
245
- useImperativeHandle ( forwardedRef , ( ) => ( {
246
- reset : ( ) => reset ( )
247
- } ) ) ;
248
-
249
- const reset = ( ) => {
250
- setInitialPositions ( trackSize . value . width ) ;
251
- onReset ?.( ) ;
252
- } ;
253
-
254
- const setInitialPositions = ( trackWidth : number ) => {
240
+ const setInitialPositions = useCallback ( ( trackWidth : number ) => {
255
241
validateValues ( props ) ;
256
242
257
- const defaultThumbPosition = getOffsetForValue (
258
- useRange ? initialMinimumValue : value ,
243
+ const defaultThumbPosition = getOffsetForValue ( useRange ? initialMinimumValue : value ,
259
244
trackWidth ,
260
245
minimumValue ,
261
246
maximumValue ) ;
262
247
const rangeThumbPosition = getOffsetForValue ( initialMaximumValue , trackWidth , minimumValue , maximumValue ) ;
263
248
defaultThumbOffset . value = defaultThumbPosition ;
264
249
rangeThumbOffset . value = useRange ? rangeThumbPosition : trackWidth ;
250
+ } , [ value ] ) ;
251
+
252
+ const reset = ( ) => {
253
+ setInitialPositions ( trackSize . value . width ) ;
254
+ onReset ?.( ) ;
265
255
} ;
266
256
257
+ useImperativeHandle ( forwardedRef , ( ) => ( {
258
+ reset : ( ) => reset ( )
259
+ } ) ) ;
260
+
261
+ useEffect ( ( ) => {
262
+ didValueUpdate . current = true ;
263
+ setInitialPositions ( trackSize . value . width ) ;
264
+ } , [ value , setInitialPositions ] ) ;
265
+
266
+ useEffect ( ( ) => {
267
+ if ( ! thumbStyle ) {
268
+ _thumbStyle . value = StyleUtils . unpackStyle ( defaultThumbStyle , { flatten : true } ) ;
269
+ }
270
+ // eslint-disable-next-line react-hooks/exhaustive-deps
271
+ } , [ defaultThumbStyle , thumbStyle ] ) ;
272
+
267
273
const onValueChangeThrottled = useCallback ( _ . throttle ( value => {
268
- onValueChange ?.( value ) ;
274
+ if ( ! didValueUpdate . current ) { // NOTE: fix for GradientSlider (should be removed after fix in the GradientSlider component): don't invoke onChange when slider's value changes to prevent updates loop
275
+ onValueChange ?.( value ) ;
276
+ } else {
277
+ didValueUpdate . current = false ;
278
+ }
269
279
} , 200 ) , [ onValueChange ] ) ;
270
280
271
281
const onRangeChangeThrottled = useCallback ( _ . throttle ( ( min , max ) => {
@@ -343,7 +353,7 @@ const Slider = React.memo((props: Props) => {
343
353
}
344
354
}
345
355
}
346
- } , [ ] ) ;
356
+ } , [ disabled , useRange , rangeGap , shouldBounceToStep ] ) ;
347
357
348
358
const _onSeekStart = ( ) => {
349
359
'worklet' ;
0 commit comments