Store in SwiftUI Views
The primary way to integrate TCA with SwiftUI is through theStore type, which can be observed directly in your views.
Basic View Integration
Pass stores to your SwiftUI views and observe state changes:Using @Bindable (iOS 17+)
For iOS 17 and later, use SwiftUI’s@Bindable to create bindings:
Using Perception.Bindable (iOS 13-16)
For pre-iOS 17, use the Perception framework’s@Bindable:
When using
@Perception.Bindable, you must wrap your view body in WithPerceptionTracking to properly observe state changes.Scoping Stores
Use thescope method to transform a store for child views:
Bindings
Two-Way Bindings
TCA provides special support for SwiftUI bindings through theBindableAction protocol:
Custom Bindings
Create custom bindings that send specific actions:Navigation
Sheet Presentation
Use binding scopes for sheet presentation:Alerts
Present alerts using store bindings:Confirmation Dialogs
Similar to alerts, confirmation dialogs use store bindings:Observable State
Mark your state with@ObservableState to enable observation:
Best Practices
Scope Early, Scope Often
Scope stores to the minimum state needed by each view:Use WithPerceptionTracking for iOS 13-16
When supporting pre-iOS 17, always wrap view bodies inWithPerceptionTracking:
ForEach: