Quick start
This guide will walk you through building a simple counter feature with TCA. You’ll learn how to manage state, handle actions, execute side effects, and write tests.For a more in-depth, interactive tutorial, check out Meet the Composable Architecture.
What you’ll build
You’ll build a feature that displays a number along with increment and decrement buttons. It will also have a button that fetches a random fact about the current number from an API and displays it.Step 1: Create the reducer
First, create a new type annotated with the@Reducer macro. This will house the domain and behavior of your feature.
Step 2: Define the state
Inside your reducer, define aState type that describes all the data your feature needs. Apply the @ObservableState macro to take advantage of Swift’s observation tools.
count: The current number to displaynumberFact: An optional string for the fetched fact
Step 3: Define actions
Define anAction enum that represents all the actions that can happen in your feature.
- User interactions: increment, decrement, and fact button taps
- System events: receiving the fact response from the API
Step 4: Implement the reducer
Implement thebody property to describe how state evolves when actions occur.
Actions that don’t need to execute side effects return
.none. The fact button returns a .run effect that performs an asynchronous API request.Step 5: Build the view
Create a SwiftUI view that holds aStoreOf<Feature> to observe state and send actions.
Step 6: Create the store
At your app’s entry point, construct a store with initial state and the reducer.Step 7: Test your feature
TCA makes testing straightforward. UseTestStore to assert how your feature evolves over time.
Step 8: Extract dependencies
To make your feature testable, extract the API dependency so you can control it in tests. Now your test will compile and run! The test dependency is automatically used in tests, while the live dependency runs in the app.What you learned
In this quick start guide, you:Defined state
Created a state type with
@ObservableState to hold feature dataHandled actions
Defined an action enum for user interactions and system events
Implemented logic
Used the
Reduce reducer to evolve state based on actionsExecuted effects
Used
.run to perform asynchronous API requestsBuilt the UI
Created views that observe the store and send actions
Wrote tests
Used
TestStore to verify feature behavior with mocked dependenciesNext steps
You’ve built your first TCA feature! Here’s what to explore next:Navigation
Learn how to navigate between features and manage navigation state
Composition
Break down large features into smaller, reusable components
Testing
Deep dive into testing strategies and best practices
Examples
Explore real-world example applications built with TCA