MasterOnce
HomeBlogHow-To
3 min readMar 10, 2026
Swift

How to Use @State in SwiftUI

Store local view state with @State.

Steps

  1. Paste the view into a SwiftUI file.
  2. Set it as a preview or root view.
  3. 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.