The Composable Architecture (Part 2)

State

Oliver Perez
2 min readMar 20, 2023

In The Composable Architecture (TCA), State plays a crucial role in managing and organizing the data within the application. To effectively use TCA, it’s essential to understand the concept of State and how it’s used in the architecture.

Definition and Purpose of State

State in TCA represents the entire state of the application or a specific part of it. It stores the data and information required by the application to function correctly. The State in TCA is immutable, meaning that it cannot be modified directly. Instead, changes to the state are made through actions and reducers, which enforce a consistent and predictable way of updating the state.

The primary purpose of State in TCA is to:

  1. Hold the data necessary for the application to function and display its user interface.
  2. Provide a single source of truth for the application state, ensuring that updates are predictable and consistent.
  3. Enable the composition of smaller states into a larger application state, allowing for modular and maintainable code.

Creating and Using State Structures

State in TCA is typically defined as a Swift struct, containing properties that store the relevant data. To define a State, follow these steps:

  1. Create a Swift struct to represent the state.
  2. Add properties to the struct to store the necessary data.
  3. If needed, create nested structs to represent sub-states for different parts of the application.

For example, consider an application that manages a list of tasks. The State for this application could be defined as follows:

struct AppState {
var tasks: [Task]
var selectedTask: Task?
}

struct Task {
let id: UUID
var title: String
var isCompleted: Bool
}

In this example, the AppState struct represents the entire application state, containing an array of tasks and an optional property for the selected task. The Task struct represents an individual task with an identifier, title, and completion status.

Nested Types and State Composition

TCA encourages the composition of smaller states into a larger application state. This allows developers to create modular and maintainable code by breaking down the application state into smaller, more manageable pieces.

To compose states in TCA, you can create nested structs within the main state struct. Each nested struct represents a sub-state for a specific part of the application. These sub-states can be combined to form the complete application state.

For instance, if the task management application also includes user authentication, you could define separate structs for the authentication state and the task state, composing them within the main application state:

struct AppState {
var authState: AuthState
var taskState: TaskState
}

struct AuthState {
var isAuthenticated: Bool
var user: User?
}

struct TaskState {
var tasks: [Task]
var selectedTask: Task?
}

// Task and User structs omitted for brevity

That's it for now, in the following stories we'll explore in detail the rest of the components of the TCA.

--

--