I'm currently trying to present a modal view by pressing a button in the context menu. This works, but the code which should present the modal view is called twice and this is a problem, because I'm calling some networking requests.
Here is my currently demo project (without networking stuff):
This is the view which is called on app start.
struct ContentView: View {
    @State var isModal: Bool = false
    var body: some View {
        Group {
            Text("Main view")
        }.contextMenu {
            Button("Present Detail") { self.isModal = true }.sheet(isPresented: $isModal) {
                DetailView()
            }
        }
    }
}
This is the simple detail view
struct DetailView: View {
    var body: some View {
        Text("Detail View")
    }
}
So if I place a breakpoint at the line where the DetailView() is instantiated, I see that this part is called twice. Is there a better solution to present the modal view, without being instantiated multiple times?
Use instead
var body: some View {
    Group {
        Text("Main view")
    }.contextMenu {
        Button("Present Detail") { self.isModal = true }
    }.sheet(isPresented: $isModal) {
        DetailView()
    }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With