3 min readMar 10, 2026
SwiftHow to Use @State in SwiftUI
Store local view state with @State.
Steps
- Paste the view into a SwiftUI file.
- Set it as a preview or root view.
- Update state or bindings to match your data.
Copy and paste
import SwiftUI
struct CounterView: View {
@State private var count = 0
var body: some View {
Button("Count: (count)") {
count += 1
}
.padding()
}
}Notes
- Use @State for local values and bindings for child views.
- Extract subviews when the body grows.
