Effects and Dependencies
Handle side effects and inject dependencies.
Why Effects Exist
Reducers must stay pure: given a state and an action, they only mutate state. But real apps need side effects like network requests and timers. TCA models these as Effects the reducer returns, keeping the mutation pure while the async work runs separately.
This separation is what makes features testable.
import ComposableArchitecture
// Reducer stays pure; side effects are values it returns.
// Returning .none means no further work.Returning .none
When an action only changes state and needs no follow-up work, the reducer returns .none. This is the most common effect and signals there is nothing asynchronous to do.
You saw this already; it is the baseline every branch falls back to.
import ComposableArchitecture
Reduce { state, action in
switch action {
case .incrementTapped:
state.count += 1
return .none
}
}All lessons in this course
- State, Action, and Reducer
- The Store and SwiftUI Integration
- Effects and Dependencies
- Composing and Testing Features