Why Views Need State
Understand why SwiftUI views are immutable.
Views Describe, They Don't Hold
A SwiftUI view is a lightweight description of your UI for one moment. It is recreated constantly, so it can't simply store changing values like a normal object. 📐
Structs Are Immutable
Your views are structs, and SwiftUI treats their stored properties as read-only inside body. You cannot just reassign a property and expect the screen to react.
struct CounterView: View {
var count = 0
var body: some View {
Text("Count: \(count)")
}
}All lessons in this course
- Why Views Need State
- Declaring @State Properties
- Building a Tap Counter
- Toggling Visibility with State