Web: value={0} with a negative minimumValue renders the thumb at the minimum
維護者通常 2 天內回覆
評估
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 新手友好度
- 78/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 活躍
- 技術堆疊
- react-native, typescript
研究方向
從 package/src/RNCSliderNativeComponent.web.tsx 開始,特別查看第 81 行附近的初始狀態,以及第 163-171 行附近的重新同步 effect。重現提供的負最小值範例,然後確認明確指定的值 value={0} 會以零呈現,而非受控情況不會意外重設。
由索引模型根據 Issue 內容生成。
描述
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.
- 主要語言
- TypeScript
- 星號
- 1.4k
- 分支
- 297
- 平均合併
- 3 天 4 小時
- 30 天內合併 PR
- 11
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
callstack/react-native-slider 的其他 Issue
-
Missing peerDependencies for react and react-native causes duplicate React instances in pnpm monorepos可能已有人在做 @patrickwehbe 於 130 天前認領。 未關閉bug report
難度 1/5 1 小時以內 新手友好度 68/100
callstack/react-native-slider#769 ·
維護者通常 2 天內回覆
-
Web: step snapping ignores minimumValue可能已有人在做 @RaphaelFakhri 於 7 天前認領。 未關閉platform: Web v6
難度 2/5 1-3 小時 新手友好度 35/100
callstack/react-native-slider#840 ·
維護者通常 2 天內回覆
-
bug report platform: iOS v6
難度 4/5 3-5 天 新手友好度 42/100
callstack/react-native-slider#782 · 6 則留言 ·
維護者通常 2 天內回覆
-
bug report
難度 3/5 1-2 天 新手友好度 35/100
callstack/react-native-slider#767 ·
維護者通常 2 天內回覆
-
bug report platform: iOS v6
難度 4/5 3-5 天 新手友好度 45/100
callstack/react-native-slider#758 · 5 則留言 ·
維護者通常 2 天內回覆
查看 callstack/react-native-slider 的全部 Issue
相似的 Issue
-
community first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
難度 1/5 1 小時以內 新手友好度 82/100
lingdojo/kana-dojo#33286 · 1 則留言 · 5 個 reaction ·
維護者通常 1 天內回覆
-
friction
難度 1/5 1 小時以內 新手友好度 77/100
kentcdodds/kody#4499 ·
-
bug
難度 2/5 1-3 小時 新手友好度 76/100
hemilabs/ui-monorepo#2605 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 6 天內回覆
-
build.yml: macOS arm64 DMG fails, cargo missing on the macos-26 runner image可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉
難度 2/5 1-3 小時 新手友好度 78/100
getopenscreen/openscreen#1423 ·
維護者通常 1 天內回覆