Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onValueChange triggers an extra time after onSlidingCompleted on Android #569

Open
henrikvilhelmberglund opened this issue Jan 4, 2024 · 2 comments
Labels
bug report Something isn't working platform: Android Issue related to Android platform

Comments

@henrikvilhelmberglund
Copy link

Environment

  • react-native info output:
// react-native info
  • are you using the new architecture?
    I'm guessing no.

  • which version of react & react-native are you using?
    "expo": "~49.0.15",
    "react": "18.2.0",
    "react-native": "0.72.6",
    "@react-native-community/slider": "4.4.2"

Description

onValueChange triggers an extra time after onSlidingCompleted on Android.

Inside of onSlidingCompleted I'm setting the slider value to a specific value when the thumb button is pressed if the current value and saved previous value is the same. This works but since onValueChange is triggered an additional time after onSlidingCompleted the slider jumps back to the initial pressed position.

This can be a bit hard to see in this project because it is so small/fast but you should be able to see it flash to the value 5. I think this works correctly on iOS.

  1. Drag the slider to the right.
  2. Press the thumb button a bunch of times.

Expected: after one press of the thumb button the slider should jump to the value 5.
Result: the slider jumps to 5 and back to the original position.

Expected order:
onSlidingStart
onValueChange (if there is a value change at all)
onSlidingComplete

Actual order:
onSlidingStart
onValueChange (if there is a value change at all)
onSlidingComplete
onValueChange

Reproducible Demo

Repro: https://github.com/henrikvilhelmberglund/react-native-slider-issue-android

Code:

import { StatusBar } from "expo-status-bar";
import { StyleSheet, Text, View } from "react-native";
import Slider from "@react-native-community/slider";
import { useState } from "react";

export default function App() {
  const [sliderValue, setSliderValue] = useState(5);
  const [sliderValueStart, setSliderValueStart] = useState();
  const handleSlidingStart = (initialSliderValue) => {
    console.log("handleSlidingStart", handleSlidingStart);
    setSliderValueStart(initialSliderValue);
  };

  const handleSlidingChange = (sliderValue) => {
    console.log("handleSliderChange", handleSlidingChange);
    setSliderValue(sliderValue);
    console.log("after set slider value in change", sliderValue);
  };

  const handleSlidingComplete = (sliderValue) => {
    console.log("handleSlidingComplete", handleSlidingComplete);
    if (sliderValue !== sliderValueStart) {
      setSliderValue(sliderValue);
    } else {
      console.log("sliderValue and sliderValueStart are the same");
      // set slider value to 5
      setSliderValue(5);
      console.log("after set slider value in complete", sliderValue);
    }
  };

  return (
    <View style={styles.container}>
      <Slider
        style={{ width: 400, height: 40 }}
        minimumValue={0}
        maximumValue={23}
        step={1}
        thumbTintColor="#000000"
        minimumTrackTintColor="#222222"
        value={sliderValue}
        onSlidingStart={handleSlidingStart}
        onSlidingComplete={handleSlidingComplete}
        onValueChange={handleSlidingChange}
        tapToSeek={true}
        // maximumTrackTintColor="#000000"
      />
      <StatusBar style="auto" />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center",
  },
});
@henrikvilhelmberglund henrikvilhelmberglund added the bug report Something isn't working label Jan 4, 2024
@BartoszKlonowski BartoszKlonowski added the platform: Android Issue related to Android platform label Jan 8, 2024
@BartoszKlonowski BartoszKlonowski added this to To be analyzed in Slider-Board via automation Jan 8, 2024
@subramani01
Copy link

@henrikvilhelmberglund is this fixed? i'm having the same issue, onValueChange triggered an extra time after onSlidingComplete!

@Jmedders
Copy link

Jmedders commented Mar 28, 2024

Reproducing this still: snack (though snacks on android & iOS are broken as mentioned in #587)

As a possible fix for some until the library's updated, something that helped me was abstracting away logic where possible from onValueChange to onSlidingStart

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug report Something isn't working platform: Android Issue related to Android platform
Projects
Slider-Board
  
To be analyzed
Development

No branches or pull requests

4 participants