The Counter example demonstrates the fundamental concepts of The Composable Architecture. Itβs the perfect starting point for understanding how state, actions, and reducers work together.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/pointfreeco/swift-composable-architecture/llms.txt
Use this file to discover all available pages before exploring further.
Overview
This example shows how to:- Define state using
@ObservableState - Handle actions with a
Reducer - Build SwiftUI views that observe store changes
- Send actions from the view layer
Implementation
Key Concepts
State
TheState struct holds all the mutable data for the feature. Here, we only need a single integer count:
@ObservableState macro makes the state observable by SwiftUI, automatically updating views when state changes.
Actions
Actions represent all the ways users can interact with the feature:Reducer
The reducer handles state mutations based on actions:.none because there are no side effects to execute.
View Integration
The view observes the store and sends actions:Testing
This simple structure makes testing straightforward:Source Code
View the complete example in the TCA repository:Next Steps
- Learn about composition by embedding multiple counters
- Explore effects for side effects
- See testing for comprehensive test coverage