SwiftUI navigationDestination will make child view's stateObject init multi times with sheet modifier.

Below is my sample code. On the Home page, when I click "show sheet," the sheet page expands, and the StateObject inside the sheet is initialized once. However, when I click "show Fullscreen" and then click "show sheet" inside the fullscreen page, the sheet gets initialized twice.

However, if I remove navigationDestination, this issue does not occur.

This problem causes the network request in the sheet page to be triggered multiple times.

Can someone tell me the reason?

enum TestRouter: String, Hashable {
    case test
    
    var targetView: some View {
        Text("test")
    }
    
    var title: String {
        return "test title"
    }
}

@MainActor
struct NavigationInnerView<Content>: View where Content: View {
    var contentView: () -> Content
    
    @MainActor public init(@ViewBuilder contentView: @escaping () -> Content) {
        self.contentView = contentView
    }
    
    var body: some View {
        NavigationStack() {
            contentView()
                .navigationDestination(for: TestRouter.self) { route in
                    route.targetView
                }
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}


struct ContentView: View {
    @State var showFullScreen: Bool = false
    @State var showSheet: Bool = false
    var contentView: some View {
        VStack {
            VStack {
                Text("Home")
                
                Button {
                    showFullScreen = true
                } label: {
                    Text("show fullscreen")
                }
                
                Button {
                    showSheet = true
                } label: {
                    Text("show sheet ")
                }
            }
        }
    }
    
    var body: some View {
        NavigationInnerView {
            contentView
                .fullScreenCover(isPresented: $showFullScreen) {
                    NavigationInnerView {
                        FullScreenContentView()
                    }
                }
                .sheet(isPresented: $showSheet) {
                    NavigationInnerView {
                        SheetContentView()
                    }
                }
        }
    }
}

class FullScreenViewModel: ObservableObject {
    @Published var content: Bool = false
    init() {
        print("Full Screen ViewModel init")
    }
}

struct FullScreenContentView: View {
    @Environment(\.dismiss) var dismiss
    @State var showSheet: Bool = false
    @StateObject var viewModel: FullScreenViewModel = .init()
    
    init() {
        print("Full screen view init")
    }
    
    var body: some View {
        VStack {
            Text("FullScreen")
            Button {
                dismiss()
            }label: {
                Text("dismiss")
            }
            Button {
                showSheet = true
            } label: {
                Text("show sheet")
            }
        }
        .sheet(isPresented: $showSheet) {
            NavigationInnerView {
                SheetContentView()
            }
        }
    }
}

class SheetViewModel: ObservableObject {
    @Published var content: Bool = false
    init() {
        print("SheetViewModel init")
    }
}

struct SheetContentView: View {
    @Environment(\.dismiss) var dismiss
    @StateObject var viewModel = SheetViewModel()
    
    init() {
        print("sheet view init")
    }
    
    var body: some View {
        Text("Sheet")
        Button {
            dismiss()
        } label: {
            Text("dismiss")
        }
    }
}

#Preview {
    ContentView()
}

@zkhCreator Could you please verify and test this on iOS & iPadOS 18.3 and respond here if you're still able to reproduce the issue.

how can I download iOS & iPadOS 18.3 ? I can't find it on my xcode or website

how can I download iOS & iPadOS 18.3 ? I can't find it on my xcode or website @DTS Engineer

SwiftUI navigationDestination will make child view's stateObject init multi times with sheet modifier.
 
 
Q