Overview
StackState and StackAction are the core types for implementing stack-based navigation in TCA. StackState is a collection that holds the state of all screens in a navigation stack, while StackAction represents actions that can be sent to or from stack elements.
StackState
Declaration
Usage
UseStackState to model navigation stack state in your feature:
Properties
ids
Subscripts
Access by ID
Access by Case
Methods
pop(from:)
id from the stack, and all elements after it:
pop(to:)
id:
Collection Operations
StackState conforms to RandomAccessCollection and RangeReplaceableCollection, so you can use standard collection operations:
StackAction
Declaration
Cases
element
popFrom
push
NavigationLink(state:).
Usage Example
StackElementID
Declaration
Testing with IDs
In tests, you can use integer literals to reference stack element IDs:StackActionOf
Type Alias
Key Points
- Type-Safe Navigation:
StackStateenforces type safety for all navigation operations - Automatic Cleanup: Effects are automatically cancelled when elements are popped from the stack
- Deterministic IDs: Stack element IDs are predictable in tests for easier testing
- Parent Intercepts: The parent reducer can intercept and respond to child actions before they’re processed
- Deep Linking: You can programmatically build deep navigation stacks by appending multiple elements
Related Types
NavigationLink(state:)- SwiftUI view for triggering navigationforEach- Reducer operator for integrating stack state@Presents- Macro for tree-based navigationPresentationState- Type for optional navigation state