Created
October 19, 2020 06:20
-
-
Save Kievkao/db54249c9184e2593afa9e095a2b04aa to your computer and use it in GitHub Desktop.
OnChange handler for Binding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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