Navigate to same view in SwiftUI

Viewed 595

So I'm trying to navigate from one view to another in SwiftUI and have stumbled upon a problem.

My views looks like this and trying to navigation from one DetailView to another DetailView but as soon I push the navigation link I'm forced back to the first DetailView.

Any ideas on how to achieve navigation to the same view in SwiftUI?

ListView.swift

struct ListView: View {
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: DetailView(data: data)) {
                    Text("Navigate")
                }
            }
        }
    }
}

DetailView.swift

struct DetailView: View {
    var body: some View {
        VStack {
            NavigationLink(destination: DetailView(data: otherData)) {
                Text("Navigate")
            }
        }
    }
}

EDIT:

Remove the NavigationView in DetailView as pointed out below but still facing the same issue.

1 Answers

Remove NavigationView from DetailView, there should be only one in stack, so when you navigate all new links are opened in original NavigationView:

struct DetailView: View {
    var body: some View {
         VStack {
             NavigationLink(destination: DetailView(data: otherData)) {
                 Text("Navigate")
             }
         }
    }
}
Related