I have a view with @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
for a custom dismiss action based on a published value around some logic like this:
.onAppear(perform: {
viewModel.cancellable = viewModel.$shouldPopBack.sink(receiveValue: { shouldPopBackToHome in
if shouldPopBackToHome {
presentationMode.wrappedValue.dismiss()
}
})
})
This view also has another option to present a sheet
Button(action: {
shouldNavigateToQRCodeScanner = true
}, label: {
Text("Scan QR code")
.padding(.horizontal)
}).sheet(isPresented: $shouldNavigateToQRCodeScanner, content: {
QRCodeScannerView()
})
The problem here is when I have @Environment(\.presentationMode)
the QRCodeScannerView
is been initialized twice, when I remove the presentationMode it works fine.
Update I've tried the same behaviour in a test project and it's the same
struct ContentView: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
@State var isPresentd: Bool = false
var body: some View {
NavigationView {
Button(action: {
isPresentd = true
}, label: {
Text("Navigate to Second View")
})
.sheet(isPresented: $isPresentd, content: {
SecondView()
})
}
}
}
struct SecondView: View {
init() {
print("Initialized")
}
var body: some View {
Text("Second View")
}
}
It has the same issue