SwiftUI - how to avoid navigation hardcoded into the view?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Hardcoded navigation makes SwiftUI views harder to reuse because the view decides not only what it displays, but also where the app goes next. A better design is to treat navigation as state or intent owned by a parent, router, or coordinator so child views stay focused on UI and user actions.
Why Hardcoded Navigation Becomes a Problem
A view such as this is tightly coupled:
That is fine for a quick prototype, but it becomes awkward when:
- the destination changes by feature flag or user role
- the same button appears in multiple flows
- navigation must be triggered after async work
- you want to test the view without bringing the full destination graph along
The deeper issue is ownership. The child view knows too much about the app's navigation structure.
Pass Intent Upward Instead of Embedding Destinations
A simple first improvement is to make the child view emit an action and let the parent decide what navigation should happen.
Now the parent owns the navigation:
This small change already makes ProfileCard reusable outside this one flow.
Model Navigation as Route State
For more complex apps, use explicit route state rather than many independent booleans.
Then manage a path in the parent:
This scales much better than embedding NavigationLink destinations directly into many leaf views.
Environment-Based Routers Can Help
If many distant views need to trigger navigation, a router object can reduce repeated path plumbing.
Inject it at the root:
And use it in child views:
This keeps navigation centralized while still letting deep views express intent.
Avoid Putting Business Logic in the Router
A router should know about routes, not business decisions. If a navigation action depends on loading data or checking permissions, that work should usually happen in a view model or parent feature layer first, then the route is emitted afterward.
That separation prevents the router from turning into a giant global decision object.
NavigationLink Is Still Fine in the Right Place
The goal is not to ban NavigationLink. It is to use it where the destination is genuinely a local UI concern.
For example, a simple static settings screen can still use:
The architecture only becomes a problem when hardcoded navigation prevents reuse or couples views to flows they should not own.
Common Pitfalls
- Embedding destination views directly inside reusable leaf components.
- Replacing one hardcoded
NavigationLinkproblem with a giant all-knowing global router. - Using many separate boolean flags when a route enum would describe the navigation state more clearly.
- Letting child views decide business rules and navigation at the same time.
- Avoiding all local
NavigationLinkusage even when the destination is genuinely simple and stable.
Summary
- Hardcoded navigation makes SwiftUI views less reusable and harder to test.
- A better pattern is to lift navigation state or navigation intent to a parent, router, or coordinator layer.
- '
NavigationStackwith a route enum scales better than many embedded destination definitions.' - Child views should usually emit intent, not own app-level routing decisions.
- Use direct
NavigationLinkonly when the navigation is truly local and stable.
Related reading
- SwiftUI - Multiple Buttons in a List row
- SwiftUI - Multiple Buttons in a List row
- SwiftUI app life cycle iOS14 where to put AppDelegate code?
- SwiftUI app life cycle iOS14 where to put AppDelegate code?
- SwiftUI Binding Initialize
- SwiftUI can't tap in Spacer of HStack
- SwiftUI can't tap in Spacer of HStack
- SwiftUI. How to change the placeholder color of the TextField?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.