Skip to content

Instantly share code, notes, and snippets.

@Kievkao
Created October 19, 2020 06:20
Show Gist options
  • Save Kievkao/db54249c9184e2593afa9e095a2b04aa to your computer and use it in GitHub Desktop.
Save Kievkao/db54249c9184e2593afa9e095a2b04aa to your computer and use it in GitHub Desktop.
OnChange handler for Binding
extension Binding {
func onChange(_ handler: @escaping (Value) -> Void) -> Binding<Value> {
Binding(
get: { self.wrappedValue },
set: { newValue in
self.wrappedValue = newValue
handler(newValue)
}
)
}
}
struct ContentView: View {
@State private var rating = 0.0
var body: some View {
Slider(value: $rating.onChange(sliderChanged))
}
func sliderChanged(_ value: Double) {
print("Rating changed to \(value)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment