← Back to archive

Development · SwiftUI

Managing local state with @State

Understand how @State stores view-owned values and triggers updates when they change.

Read the Korean original on Velog ↗English translation

Development archive cover

SwiftUI views are value types, yet the numbers and selections they display keep changing. @State connects persistent state storage with the updates a view needs.

@State in a structure

A view's body is computed from its current data. @State gives SwiftUI a place to manage a value outside the transient view value and update dependent views when it changes. Declare state as private to keep ownership within the view.

struct StateStudy: View {
    
    // Observe value changes
    @State private var count: Int = 0
    
    var body: some View {
        VStack(spacing: 24) {
            Text("Current Count is \(count)")
            Button("Click to Count") {
                count += 1
            }
        }
    }
}

← Back to archive