What's wrong with navigationDestination with binding on ios 16? I have an infinite loop in nested navigation with binding and navigation stack
struct Visit: Identifiable, Hashable {
var id: String
let name: String
}
struct ClientsListView: View {
@Binding var selected: Visit
var body: some View {
Text(selected.name)
.navigationTitle("Clients")
}
}
struct VisitFormView: View {
@Binding var selected: Visit
var body: some View {
NavigationLink("Select client", destination: {
ClientsListView(selected: $selected)
})
.navigationTitle("Visit form")
.onAppear {
print("form on appear")
}
}
}
class VisitsViewModel: ObservableObject {
@Published var list: [Visit] = {
(0...30).map { Visit(id: $0.formatted(), name: "Test \($0)") }
}()
}
struct VisitsView: View {
@StateObject var vm = VisitsViewModel()
var body: some View {
List {
ForEach(0..<vm.list.count, id:\.self) { index in
NavigationLink(value: index) {
Text(vm.list[index].name)
}
}
}
.navigationDestination(for: Int.self) { id in
let _ = print("VisitsView navigationDestination \(Date())") // infinite call print
VisitFormView(selected: $vm.list[id])
}
.navigationTitle("Visits")
}
}
struct MainView: View {
var body: some View {
TabView {
NavigationStack {
VisitsView()
}
}
}
}
after I tap "select client" the result is an infinite loop, with a memory leak and 100% CPU
if I will remove binding from VisitFormView problem goes away. if replace navigationDestination to init(@ViewBuilder destination: () -> Destination, @ViewBuilder label: () -> Label) will work. I can resolve problems with the environment, but I want to understand what happens, and why loops. any help?
navigationDestination
does not work withBinding
you have to useNavigationLink
withdestination
in it, hopefully there will be a better solution in a couple of weeks but as of no there is no solution for this just workarounds that half work.