Web: value={0} with a negative minimumValue renders the thumb at the minimum
Évaluation
- Difficulté
- 2/5
- Temps estimé
- 1-3 heures
- Accessibilité débutants
- 78/100
- Type d'issue
- Bug
- Clarté
- Clairement spécifiée
- Activité
- Active
- Stack technique
- react-native, typescript
Piste de recherche
Commencez dans package/src/RNCSliderNativeComponent.web.tsx, en particulier au niveau de l’état initial vers la ligne 81 et de l’effet de resynchronisation vers les lignes 163-171. Reproduisez l’exemple fourni avec un minimum négatif, puis vérifiez qu’une valeur explicite value={0} s’affiche à zéro, tandis que le cas non contrôlé ne se réinitialise pas de manière inattendue.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Environment
@react-native-community/slider: 5.2.1 (also present onmainat the time of writing)- Platform: web (
react-native-web0.21.2) - react: 19.2.3 · react-native: 0.86.3 · Expo SDK 57
- New architecture: yes (Expo
newArchEnabledis unconditional in SDK 57) — but this is a web-only code path, so the architecture is not relevant
Description
On web, a slider rendered with value={0} and a negative minimumValue paints its thumb at the minimum instead of at zero.
The value itself is fine — onValueChange reports correctly, and every number the app derives from the slider is right. Only the thumb position is wrong, and it stays wrong until the user drags it, at which point it starts behaving. So the failure looks like a rendering quirk rather than a state bug, which makes it easy to dismiss.
It is quite easy to hit in practice: any bipolar control that rests at zero does. Mine is a turntable-style pitch fader (-8 … +8, resting at 0), which means it was wrong on every load.
Reproducible Demo
import Slider from '@react-native-community/slider';
// Thumb renders hard left. Expected: centred.
<Slider minimumValue={-8} maximumValue={8} value={0} />
// Both of these are fine, which isolates it to the value being exactly zero
// against a negative minimum:
<Slider minimumValue={-8} maximumValue={8} value={0.1} />
<Slider minimumValue={0} maximumValue={16} value={8} />
Cause
Two things combine, both in package/src/RNCSliderNativeComponent.web.tsx.
1. The initial state is seeded with || (L81):
const [value, setValue] = React.useState(initialValue || minimumValue);
0 is falsy, so value={0} seeds the internal state at minimumValue rather than at 0.
This cannot be fixed with ?? alone, because value is already defaulted at the destructure (L55):
value: initialValue = 0,
so by the time that line runs, "no value prop" and value={0} are indistinguishable — both are 0.
2. The resync effect cannot recover it (L163-L171):
const lastInitialValue = React.useRef<number>(0);
// ...
if (initialValue !== lastInitialValue.current) {
lastInitialValue.current = initialValue;
const newValue = updateValue(initialValue);
animationValues.val.setValue(newValue);
}
The ref starts at 0, so for initialValue === 0 the guard evaluates 0 !== 0 — false. The effect never runs and the wrong initial state is never corrected.
I appreciate the guard is deliberate; the comment above it explains that it exists because initialValue gets defaulted to 0 by Slider, and without it the value resets constantly when the slider is uncontrolled. Any fix wants to keep that property.
Suggested fix
Distinguish "no value prop" from value={0} by not defaulting at the destructure, and track the same distinction in the ref:
value: initialValue, // no ` = 0`
// ...
const [value, setValue] = React.useState(initialValue ?? minimumValue);
const lastInitialValue = React.useRef<number | undefined>(initialValue);
// ...
if (initialValue !== undefined && initialValue !== lastInitialValue.current) {
lastInitialValue.current = initialValue;
const newValue = updateValue(initialValue);
animationValues.val.setValue(newValue);
}
That keeps the uncontrolled case from being reset (the effect still does nothing when no value is passed) while letting an explicit value={0} through.
I have not sent a PR since the change touches the uncontrolled path and you will have a clearer view than I do of what that should do; happy to open one if the approach looks right.
Workaround
For anyone hitting this before it is fixed: shift the domain so zero is not the resting value, and convert at the boundary.
// fader units 0 … 2R, presented as -R … +R
<Slider
minimumValue={0}
maximumValue={range * 2}
value={pitch + range}
onValueChange={(v) => setPitch(v - range)}
/>
The only value that still lands on 0 is the control fully down, where minimumValue is also 0, so the falsy branch produces the correct result anyway.
- Langage dominant
- TypeScript
- Étoiles
- 1.4k
- Forks
- 295
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de callstack/react-native-slider
-
Missing peerDependencies for react and react-native causes duplicate React instances in pnpm monoreposPeut-être pris @patrickwehbe l’a pris il y a 120 jours. Ouvertebug report
Difficulté 1/5 Moins d'une heure Accessibilité débutants 68/100
callstack/react-native-slider#769 ·
-
bug report platform: iOS v6
Difficulté 4/5 3-5 jours Accessibilité débutants 42/100
callstack/react-native-slider#782 · 6 commentaires ·
-
iOS: negative min value and 0 as maximum value turns maximum to 1Peut-être pris @huytdps13400 l’a pris il y a 13 jours. Ouvertebug report
Difficulté 3/5 1-2 jours Accessibilité débutants 65/100
callstack/react-native-slider#780 · 1 commentaire ·
-
bug report
Difficulté 3/5 1-2 jours Accessibilité débutants 35/100
callstack/react-native-slider#767 ·
-
bug report platform: iOS v6
Difficulté 4/5 3-5 jours Accessibilité débutants 45/100
callstack/react-native-slider#758 · 5 commentaires ·
Toutes les issues de callstack/react-native-slider
Issues similaires
-
The inspector never shows the instances field, so scaling out is unreachablePeut-être pris @Mrkewl l’a pris aujourd’hui. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
xevrion/breakscale#96 ·
Les mainteneurs répondent en général sous 4 jours
-
Object parameters lose their structure when resources is enabledPeut-être pris @asts-top l’a pris aujourd’hui. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100
Les mainteneurs répondent en général sous 2 jours
-
Difficulté 2/5 1-3 heures Accessibilité débutants 65/100
rossant/awesome-math#199 ·
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
jaegertracing/jaeger-ui#4618 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
mifi/lossless-cut#3125 ·
Les mainteneurs répondent en général sous 1 jour