0Pricing
Swift Academy · Lesson

Composing and Testing Features

Combine reducers and test exhaustively.

Composing Features

Real apps are built from many small features. TCA lets a parent feature embed child features so each stays focused, then combines them into a larger whole. The tools for this are Scope and embedding child state and actions.

Composition is what gives The Composable Architecture its name.

import ComposableArchitecture

// A parent feature embeds child State and Action,
// then uses Scope to run the child's reducer.

Embedding Child State

The parent's State holds the child's state as a property. The parent's Action enum has a case wrapping the child's actions. This nesting mirrors how the UI nests.

Here a parent holds a counter child.

import ComposableArchitecture

@Reducer
struct AppFeature {
    @ObservableState
    struct State: Equatable {
        var counter = CounterFeature.State()
    }
    enum Action {
        case counter(CounterFeature.Action)
    }
    var body: some ReducerOf<Self> { EmptyReducer() }
}

All lessons in this course

  1. State, Action, and Reducer
  2. The Store and SwiftUI Integration
  3. Effects and Dependencies
  4. Composing and Testing Features
← Back to Swift Academy