Modularised Navigation in SwiftUI — An Enum-based Approach

<p>In iOS 16, SwiftUI finally allows us to properly encapsulate navigation without being forced to pass a ton of bindings or using&nbsp;<code>NavigationLink</code>&nbsp;directly in UI.</p> <p>One thing that always introduces additional challenges, though, is modularisation.</p> <p>In a best-case scenario, every module functions independently and has zero dependencies on other modules (in the same layer).</p> <p>While it is possible to achieve that, there is always the need to navigate from one module to the other.</p> <p>One approach that elegantly solves this issue is the use of enums for internal and external navigation.</p> <h1>Concept</h1> <p>The App (or parts of it) are bundled within a single router. The entry point (App/Root View/TabViews) creates and holds the&nbsp;<code>NavigationPath</code>is and passes it to a&nbsp;<code>NavigationStack</code>&nbsp;. Since&nbsp;<code>NavigationPath</code>is a reference type, we can then create our main router and pass to it that very same path. Now all the main router needs are some operations for pushing and popping views.</p> <p><a href="https://betterprogramming.pub/modularised-navigation-in-swiftui-an-enum-based-approach-13028acd01ae">Visit Now</a></p>