Skip to main content
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.

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

The State struct holds all the mutable data for the feature. Here, we only need a single integer count:
The @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:
Each action modifies the state and returns an effect. Here, we return .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