-
Notifications
You must be signed in to change notification settings - Fork 1
Quick Start
ChiefVenzox edited this page Jun 5, 2026
·
1 revision
This guide builds a small counter with SwiftState.
import SwiftState
struct AppState: State {
var counter: Int = 0
}State requires Codable, Equatable, and Sendable. This keeps state serializable, comparable, and safe for Swift concurrency boundaries.
enum AppAction: Action {
case increment
case decrement
}Actions describe user intent or system events. Views dispatch actions; reducers decide how state changes.
let appReducer: Reducer<AppState> = { state, action in
guard let action = action as? AppAction else { return }
switch action {
case .increment:
state.counter += 1
case .decrement:
state.counter -= 1
}
}Reducers are synchronous and predictable. They mutate the inout state for the action they understand.
@StateObject private var store = Store(
initialState: AppState(),
reducer: appReducer
)Create the store once near your app entry point.
struct ContentView: View {
@EnvironmentObject var store: Store<AppState>
var body: some View {
VStack(spacing: 16) {
Text("Count: \(store.state.counter)")
Button("Increment") {
store.dispatch(AppAction.increment)
}
Button("Decrement") {
store.dispatch(AppAction.decrement)
}
}
}
}Inject the store:
ContentView()
.environmentObject(store)- Use SwiftUI Bindings for
TextField,Toggle, andPicker. - Use Async Effects for API calls.
- Use Time Travel Debugger while developing.