How to change navigation destination, like in UIKit

Here is my code ultimately working in UIKit, but the same thing how can I implement it in SwiftUI? Here I can change destination pushViewController based on my requirements.

/// UIKIt Code
public struct Navigator {
    public var onLoginSuccess: (UINavigationController) -> Void = { navigationController in
        navigationController.pushViewController(UIViewController(), animated: true)
    }
}

/// Usage
var router = Navigator()
router.onLoginSuccess = { nav in
    nav.pushViewController(AnyViewController(), animated: true)
}

///Here is my SwiftUI code which is working fine with defined destination, how to accept destination to `onLogin` and `push` to that view? 

struct ContentView: View {
    var body: some View {
        NavigationView {
            HStack {
                NavigationLink(destination: Router.onLogin) {
                    Text("HOME")
                }
            }
        }
    }
}

struct Router {
    @ViewBuilder
    static var onLogin: some View {
        Text("Hello")
    }
}

Is this what you need?

struct LoginView: View {
    var loginAction = Router().onLogin(Text("Actual Detail View"))

    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: loginAction) {
                    Text("Show View")
                }
            }
        }
    }
}

extension LoginView {
    struct Router<Destination: View> {
        var onLogin:(Destination) -> Destination = { links in
            return links
        }
    }
}

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        
        var contentView = LoginView()
        contentView.loginAction =  LoginView.Router().onLogin(Text("Changed Route here .!!"))
        
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }
Terms of Service

Privacy Policy

Cookie Policy