What is navigation?
For the purposes of this documentation, we use the following definition:Navigation is a change of mode in the application.
A change of mode is when some piece of state goes from not existing to existing, or vice-versa.
Two forms of navigation
Navigation broadly falls into 2 main categories:Tree-based
Use optionals and enums to model navigation in a tree-like structure
Stack-based
Use flat collections to model navigation stacks
Tree-based navigation
Tree-based navigation uses Swift’sOptional type to represent existence or non-existence of state. When multiple states of navigation are nested, they form a tree-like structure.
Basic example
Deep linking
With tree-based navigation, deep-linking is simply constructing deeply nested state:Pros and cons
Stack-based navigation
Stack-based navigation uses collections to model the presentation of features. An entire stack of features is represented by a collection of data.Basic example
Complex navigation
Stack-based navigation easily handles recursive paths:Pros and cons
Choosing an approach
Most real-world applications use both approaches:1
Start with stack-based for main flow
Use
NavigationStack and stack-based navigation for your app’s primary navigation flow.2
Use tree-based for modals and auxiliary UI
Within each feature in the stack, use tree-based navigation for sheets, popovers, alerts, and other modal presentations.
3
Consider your priorities
- Need finite, well-defined paths? → Tree-based
- Need recursive or complex paths? → Stack-based
- Need easy integration testing? → Tree-based
- Need fully decoupled features? → Stack-based
Navigation tools
The Composable Architecture provides several tools for implementing navigation:Tree-based tools
@Presentsmacro for optional statePresentationActionfor handling child actionsifLetreducer operator for integration- Navigation view modifiers for sheets, popovers, alerts, etc.
Stack-based tools
StackStatefor modeling stack collectionsStackActionfor stack-related actionsforEachreducer operator for integrationNavigationStackinitializer tuned for TCA
Next steps
Tree-based Navigation
Learn the details of implementing tree-based navigation
Stack-based Navigation
Learn the details of implementing stack-based navigation
Dismissal
Learn how to dismiss features from child domains
Testing Navigation
Learn how to test navigation flows