Swift UI @FocusState triggering NavigationLink Twice

Viewed 22

What am I doing wrong here? Tapping on the navigation link triggers the navigation, then the focusState value updates, causing body to run and trigger the navigation link again.

How do I prevent the link from being triggered twice causing my destination views init to fire twice?

import SwiftUI

struct ContentView: View {
    @State var text: String = "text"
    
    @FocusState var focussed: Bool
    
    @State var isActive: Bool = false

    var body: some View {
        NavigationView {
            List {
                TextField("", text: $text)
                    .focused($focussed)
                    .onChange(of: focussed) { _ in }
                
                let _ = Self._printChanges()
                NavigationLink("Tap Me", destination: MyViewTwo(), isActive: $isActive)
            }
        }
    }
}

struct MyViewTwo: View {
    init() {
        print("Init Called")
    }
    
    var body: some View {
        Text("Hello View 2")
    }
}
0 Answers
Related