Skip to main content

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

A list of data representing the content of a navigation stack.

Usage

Use StackState to model navigation stack state in your feature:

Properties

ids

An ordered set of identifiers, one for each stack element. You can use this to iterate over elements with their IDs:

Subscripts

Access by ID

Access the value associated with a given ID:

Access by Case

Access a specific case of an enum element:

Methods

pop(from:)

Pops the element corresponding to id from the stack, and all elements after it:

pop(to:)

Pops all elements that come after the element corresponding to id:

Collection Operations

StackState conforms to RandomAccessCollection and RangeReplaceableCollection, so you can use standard collection operations:

StackAction

Declaration

A wrapper type for actions that can be presented in a navigation stack.

Cases

element

An action sent to the associated stack element at a given identifier:

popFrom

An action sent to dismiss the stack element at the given identifier:

push

An action sent to present the given state at a given identifier. This is typically sent automatically from the view via NavigationLink(state:).

Usage Example

StackElementID

Declaration

An opaque identifier for stack elements. IDs are automatically generated when elements are added to the stack.

Testing with IDs

In tests, you can use integer literals to reference stack element IDs:
Note that IDs are generational in tests - they keep counting up even after elements are removed.

StackActionOf

Type Alias

A convenience type alias for referring to a stack action of a given reducer’s domain:

Key Points

  • Type-Safe Navigation: StackState enforces 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
  • NavigationLink(state:) - SwiftUI view for triggering navigation
  • forEach - Reducer operator for integrating stack state
  • @Presents - Macro for tree-based navigation
  • PresentationState - Type for optional navigation state

See Also