Skip to content

Commit fcc712b

Browse files
committed
fix: prevent view update warnings in Slider component
Use local state for slider value to avoid "publishing changes from within view updates" warnings. Only fire onChange callback during active editing and sync external prop updates when not editing.
1 parent 7271a53 commit fcc712b

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

ios/components/Slider/SliderView.swift

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ import SwiftUI
33
public struct SliderView<Content: View>: View {
44
@ObservedObject public var props: SliderProps
55
@State private var isFocused: Bool = false
6+
@State private var localValue: Double = 0
7+
@State private var isEditing: Bool = false
68
let content: () -> Content
79

810
public init(props: SliderProps, @ViewBuilder content: @escaping () -> Content) {
911
self.props = props
1012
self.content = content
13+
_localValue = State(initialValue: props.value)
1114
}
1215

1316
public var body: some View {
@@ -32,19 +35,33 @@ public struct SliderView<Content: View>: View {
3235
HStack {
3336
content()
3437
Slider(
35-
value: $props.value,
38+
value: $localValue,
3639
in: props.minimum ... props.maximum,
3740
step: props.step,
38-
onEditingChanged: { self.isFocused = $0 }
41+
onEditingChanged: { editing in
42+
isEditing = editing
43+
isFocused = editing
44+
if !editing {
45+
// Sync final value to props when user stops dragging
46+
props.value = localValue
47+
}
48+
}
3949
)
4050
.disabled(props.disabled)
4151
.foregroundColor(props.disabled ? .gray : .primary)
4252
.onChange(of: isFocused) { newValue in
4353
newValue ? props.onFocus?() : props.onBlur?()
4454
}
45-
.onChange(of: props.value) { newValue in
55+
.onChange(of: localValue) { newValue in
56+
// Only fire onChange during active editing
57+
guard isEditing else { return }
4658
props.onChange?(newValue)
4759
}
60+
.onChange(of: props.value) { newValue in
61+
// Sync from props only when not editing (external update)
62+
guard !isEditing else { return }
63+
localValue = newValue
64+
}
4865
}
4966
}
5067
}

0 commit comments

Comments
 (0)