State Pattern (SwiftUI)
Select local vs shared state and implement the pattern with best practices.
When to Use
- •When state is shared across multiple views or flows.
Inputs
- •State shape and lifecycle
- •Consumers and update frequency
- •Persistence or caching needs
Instructions
- •Decide local (
@State) vs shared (@StateObject,@Observable,@Environment). - •Use a view model for shared state.
- •Add derived state helpers in view models.
- •Add tests for state transitions.
Output
- •State implementation with tests and a short rationale.