SwiftUI Data Flow @State
SwiftUI Data Flow: @State
Use @State for local, view-owned state that drives UI updates when it changes.
@State
@State is a property wrapper that stores a value locally in a view and updates the UI when the value changes.
Example
import SwiftUI
struct CounterView: View {
@State private var count = 0
var body: some View {
VStack(spacing: 12) {
Text("Count: \(count)")
Button("Increment") { count += 1 }
}
.padding()
}
}
import SwiftUI
struct ContentView: View {
var body: some View { CounterView() }
}
import SwiftUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup { ContentView() }
}
}
The example above shows a counter that increments when the button is pressed.