Overview
The@Reducer macro is the primary way to define features in The Composable Architecture. It synthesizes the necessary boilerplate code to conform your type to the Reducer protocol, allowing you to focus on your feature’s logic.
When applied to a type, the macro generates:
StateandActiontype definitions from nested types or enums- Protocol conformance to
Reducer - Additional helper types for enum reducers like
CaseScope - Protocol conformance to
CaseReducerfor enum reducers
Basic Usage
Apply the@Reducer macro to a struct that defines your feature:
Enum Reducers
The@Reducer macro also supports enum-based reducers for modeling navigation destinations or mutually exclusive states:
Using with Dependencies
The@Reducer macro works seamlessly with the @Dependency property wrapper:
Using with ObservableState
Combine@Reducer with @ObservableState for SwiftUI observation:
Special Case Macros
When working with enum reducers, you can use additional macros to handle special cases:@ReducerCaseEphemeral
Marks a case as holding ephemeral state like alerts or confirmation dialogs:@ReducerCaseIgnored
Marks a case that holds plain data rather than a reducer feature:See Also
- ObservableState Macro - Make state observable for SwiftUI
- Dependency Property Wrapper - Inject dependencies into reducers
- Reducer Protocol - The Reducer protocol documentation