I am learning iOS Programming with Swift and SwiftUI. I know very little and I am very confuse about the difference between a @State
and a Binding<*>
.
If I understood it correctly, Binding
is just technically State
but it doesn't update the view. If that is the case then why would I need Binding
if I could just use State
to do the same thing?
Indicate data dependencies in a view using state, and share those dependencies with other views using bindings. The user interface of a SwiftUI app is a composition of views that form a view hierarchy.
With @State, you tell SwiftUI that a view is now dependent on some state. If the state changes, so should the User Interface. It's a core principle of SwiftUI: data drives the UI.
A binding in SwiftUI is a connection between a value and a view that displays and changes it. You can create your own bindings with the @Binding property wrapper, and pass bindings into views that require them.
Two Way Binding allows the data to be transferred to change the initial look of viewController. If we are interested in creating a button that changes color when the user presses it, we need to state keyword and Two-way binding.
Both @State
and @Binding
are property wrappers.
@State
@Binding
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With