Skip to content

Commit 3440a8b

Browse files
authored
fix(web): mutating minimumValue and/or maxiumumValue failed to update UI (#465)
* fix(web): fix an issue where mutating minimumValue and/or maxiumumValue failed to update the ui * fix(web): make sure that we never divide by 0
1 parent fa1f9ea commit 3440a8b

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

package/src/RNCSliderNativeComponent.web.tsx

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ type Event = Readonly<{
2020
};
2121
}>;
2222

23+
type AnimationValues = {
24+
val: Animated.Value;
25+
min: Animated.Value;
26+
max: Animated.Value;
27+
diff: Animated.Value;
28+
};
29+
2330
export interface Props {
2431
value: number;
2532
minimumValue: number;
@@ -72,15 +79,29 @@ const RCTSliderWebComponent = React.forwardRef(
7279
const hasBeenResized = React.useRef(false);
7380
const [value, setValue] = React.useState(initialValue || minimumValue);
7481
const lastInitialValue = React.useRef<number>();
82+
const animationValues = React.useRef<AnimationValues>({
83+
val: new Animated.Value(value),
84+
min: new Animated.Value(minimumValue),
85+
max: new Animated.Value(maximumValue),
86+
// make sure we never divide by 0
87+
diff: new Animated.Value(maximumValue - minimumValue || 1),
88+
}).current;
89+
90+
// update minimumValue & maximumValue animations
91+
React.useEffect(() => {
92+
animationValues.min.setValue(minimumValue);
93+
animationValues.max.setValue(maximumValue);
94+
// make sure we never divide by 0
95+
animationValues.diff.setValue(maximumValue - minimumValue || 1);
96+
}, [animationValues, minimumValue, maximumValue]);
7597

7698
// compute animated slider position based on animated value
77-
const animatedValue = React.useRef(new Animated.Value(value)).current;
7899
const minPercent = React.useRef(
79100
Animated.multiply(
80101
new Animated.Value(100),
81102
Animated.divide(
82-
Animated.subtract(animatedValue, new Animated.Value(minimumValue)),
83-
new Animated.Value(maximumValue - minimumValue),
103+
Animated.subtract(animationValues.val, animationValues.min),
104+
animationValues.diff,
84105
),
85106
),
86107
).current;
@@ -141,9 +162,9 @@ const RCTSliderWebComponent = React.forwardRef(
141162
if (initialValue !== lastInitialValue.current) {
142163
lastInitialValue.current = initialValue;
143164
const newValue = updateValue(initialValue);
144-
animatedValue.setValue(newValue);
165+
animationValues.val.setValue(newValue);
145166
}
146-
}, [initialValue, updateValue, animatedValue]);
167+
}, [initialValue, updateValue, animationValues]);
147168

148169
const onResize = () => {
149170
hasBeenResized.current = true;
@@ -243,13 +264,13 @@ const RCTSliderWebComponent = React.forwardRef(
243264

244265
const onTouchEnd = ({nativeEvent}: GestureResponderEvent) => {
245266
const newValue = updateValue(getValueFromNativeEvent(nativeEvent.pageX));
246-
animatedValue.setValue(newValue);
267+
animationValues.val.setValue(newValue);
247268
onSlidingComplete(newValue);
248269
};
249270

250271
const onMove = ({nativeEvent}: GestureResponderEvent) => {
251272
const newValue = getValueFromNativeEvent(nativeEvent.pageX);
252-
animatedValue.setValue(newValue);
273+
animationValues.val.setValue(newValue);
253274
updateValue(newValue);
254275
};
255276

0 commit comments

Comments
 (0)