Simply including "@Environment(\.dismiss) ..." causes multiple calls to a view's body

When I run the code below, the trace, "Called", is shown 3-4 times initially. If I click on a color row, the trace shows 9 times. Why is that?

If I comment out the line, @Environment(\.dismiss) private var dismiss, the trace shows only 1 time, as expected.

I've read a number of reports regarding dismiss() which seems to be very brittle. It often causes an infinite loop. But I need to dismiss a view. Its older counterpart, @Environment(\.presentationMode), seems to cause infinite loop at times. Are there other ways to dismiss a view without suffering these issues?

struct TestNavigationLink: View {
    @Environment(\.dismiss) private var dismiss

    var body: some View {
        
        let _ = print("Called")
        
        NavigationStack {
            List {
                NavigationLink("Mint") { ColorDetail(color: .mint) }
            }
            .navigationTitle("Colors")
        }
    } // body
    
    struct ColorDetail: View {
        var color: Color
        
        var body: some View {
            color.navigationTitle(color.description)
        }
    }
}

Hello @atluutran,

I just ran your code on an iPhone running iOS 17.4.1, it only printed "Called" once. Can you update to the latest version of iOS and see if the issue still reproduces for you?

Simply including "@Environment(\.dismiss) ..." causes multiple calls to a view's body
 
 
Q