Understanding State and Binding
State
is a topic that I wanted to cover once you had some understanding of SwiftUI. Using State
in SwiftUI simplifies our apps because we will now have one source of truth. When using State
, persistent storage is created by SwiftUI for each of our views. Here is an example of a State
property:
@State private var isDriverEnabled: Bool = false
In this book, you have seen something similar to this, but let's take the time to break it down thoroughly. By adding @State
, we tell the system that the isDriverEnabled
variable changes over time, and that views will depend on this value. Changes to @State
-wrapped properties initiate a re-rendering of the view when the values are updated. Every change is dispersed to all of the views of the children.
If you have two views inside your main view, which also needs to react to a change, these views will not use the @State
property. The topmost view always owns the State
-wrapped properties. All of the...