react-native-slider icon indicating copy to clipboard operation
react-native-slider copied to clipboard

Why does onValueChange return an array?

Open justinadkins opened this issue 2 years ago • 6 comments

Here's an example of my implementation:

<Slider
    value={2}
    step={1}
    maximumValue={4}
    onValueChange={value => {
        // why is this an array?
        value = Array.isArray(value) ? value[0] : value;
        setValue(value);
    }}
    thumbTintColor={secondaryTextGray}
    minimumTrackTintColor={secondaryTextGray}
    maximumTrackTintColor={dividerBorderGray}
/>

justinadkins avatar Apr 20 '22 23:04 justinadkins

  1. value={[0, 100]} => value will be an array

    onValueChange={(value: number[]) => {
         console.log(value[0] , '<=>', value[1])
    }}
    

  1. value={100} => value will be a number

    onValueChange={(value: number) => {
         console.log(value)
    }}
    

bahmanworld avatar Apr 21 '22 13:04 bahmanworld

That was the behavior I expected as well, the 2nd example here does not occur though. Here's a snack with what I am describing being reproduced (view the logs). https://snack.expo.dev/@justinadkins/slider-array-behavior

justinadkins avatar Apr 21 '22 14:04 justinadkins

I can confirm this. I am also always receiving an array

hirbod avatar Apr 22 '22 11:04 hirbod

Hi there, My onValueChange is also receiving an array, even when value is a single number.

jblarriviere avatar May 05 '22 09:05 jblarriviere

Also does onSlidingComplete. Actually, all the functions that receive a changed value.

Dajust avatar May 27 '22 12:05 Dajust

This does make more sense that how it works today. This would be a breaking change at this point, though so we'd have to bump to v3.

Do you all feel strongly about this change?

miblanchard avatar Jul 03 '22 18:07 miblanchard

It's not really an issue, but it would be nice if the doc didn't specify the default as number

julienripet avatar Feb 14 '23 15:02 julienripet

I've updated the types to reflect the actual callbacks are just arrays of numbers.

https://github.com/miblanchard/react-native-slider/commit/1e9b84052bcef3403484fdb86e451c9c0b196f2f

Going to close this as intended behavior at this time.

miblanchard avatar Mar 12 '23 18:03 miblanchard