Skip to content

Commit 6ce386e

Browse files
brentvatneSharcoux
andauthored
Fix #182 (#186)
* Fix value not being updated on change * Update src/js/RNCSliderNativeComponent.web.js * Fix lint warnings Co-authored-by: François Billioud <[email protected]>
1 parent 2d9a498 commit 6ce386e

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

src/js/RNCSliderNativeComponent.web.js

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
*/
88

9-
import React from 'react';
9+
import React, {useCallback} from 'react';
1010
import {View, StyleSheet} from 'react-native';
1111

1212
// import type {ViewStyleProp} from 'react-native/Libraries/StyleSheet/StyleSheet';
@@ -154,19 +154,37 @@ const RCTSliderWebComponent = React.forwardRef(
154154
},
155155
forwardedRef,
156156
) => {
157-
const onValueChange = value =>
158-
onRNCSliderValueChange &&
159-
onRNCSliderValueChange({nativeEvent: {fromUser: true, value}});
160-
const onSlidingStart = value =>
161-
onRNCSliderSlidingStart &&
162-
onRNCSliderSlidingStart({nativeEvent: {fromUser: true, value}});
163-
const onSlidingComplete = value =>
164-
onRNCSliderSlidingComplete &&
165-
onRNCSliderSlidingComplete({nativeEvent: {fromUser: true, value}});
157+
const onValueChange = useCallback(
158+
value => {
159+
onRNCSliderValueChange &&
160+
onRNCSliderValueChange({nativeEvent: {fromUser: true, value}});
161+
},
162+
[onRNCSliderValueChange],
163+
);
164+
165+
const onSlidingStart = useCallback(
166+
value => {
167+
onRNCSliderSlidingStart &&
168+
onRNCSliderSlidingStart({nativeEvent: {fromUser: true, value}});
169+
},
170+
[onRNCSliderSlidingStart],
171+
);
172+
173+
const onSlidingComplete = useCallback(
174+
value => {
175+
onRNCSliderSlidingComplete &&
176+
onRNCSliderSlidingComplete({nativeEvent: {fromUser: true, value}});
177+
},
178+
[onRNCSliderSlidingComplete],
179+
);
166180

167181
const containerSize = React.useRef({width: 0, height: 0});
168182
const containerRef = forwardedRef || React.createRef();
169183
const [value, setValue] = React.useState(initialValue || minimumValue);
184+
React.useLayoutEffect(() => updateValue(initialValue), [
185+
initialValue,
186+
updateValue,
187+
]);
170188

171189
const percentageValue =
172190
(value - minimumValue) / (maximumValue - minimumValue);
@@ -223,17 +241,20 @@ const RCTSliderWebComponent = React.forwardRef(
223241
thumbStyle,
224242
);
225243

226-
const updateValue = newValue => {
227-
// Ensure that the new value is still between the bounds
228-
const withinBounds = Math.max(
229-
minimumValue,
230-
Math.min(newValue, maximumValue),
231-
);
232-
if (value !== withinBounds) {
233-
setValue(withinBounds);
234-
onValueChange(withinBounds);
235-
}
236-
};
244+
const updateValue = useCallback(
245+
newValue => {
246+
// Ensure that the new value is still between the bounds
247+
const withinBounds = Math.max(
248+
minimumValue,
249+
Math.min(newValue, maximumValue),
250+
);
251+
if (value !== withinBounds) {
252+
setValue(withinBounds);
253+
onValueChange(value);
254+
}
255+
},
256+
[minimumValue, maximumValue, value, onValueChange],
257+
);
237258

238259
const onTouchEnd = () => {
239260
onSlidingComplete(value);

0 commit comments

Comments
 (0)