WithViewStore was a SwiftUI view that allowed you to observe store state changes and build UI that reactively updates. It has been superseded by Swift’s Observation framework and the @ObservableState macro.
Declaration
Overview
WithViewStore transformed a Store into a ViewStore, which was an ObservableObject that SwiftUI could observe. This pattern is no longer necessary with modern TCA and Swift’s Observation framework.
Legacy Usage (Deprecated)
Modern Approach (Recommended)
With@ObservableState, you can directly access store state without WithViewStore:
Parameters
Legacy WithViewStore Parameters
store: The store to observeobserve: A function that transforms the store’s state into view stateremoveDuplicates: An optional function to determine when state changes should trigger view updatescontent: A view builder that receives theViewStoreand returns the content view
Migration Guide
Step 1: Add @ObservableState
Add the@ObservableState macro to your reducer’s state:
Step 2: Remove WithViewStore
ReplaceWithViewStore with direct store access:
Step 3: Update Send Calls
ChangeviewStore.send to store.send:
Benefits of Migration
- Better Performance: Swift’s Observation framework is more efficient than
ObservableObject - Less Boilerplate: No need to wrap views in
WithViewStore - Simpler Code: Direct property access on stores
- Type Safety: Better compile-time guarantees
See Also
@ObservableState: The modern way to make state observableStore: The store type that powers TCA- Migration Guide