[Summarize the problem]
.onAppear
modifier called twice when using .navigationViewStyle(.stack)
on NavigationView.
[Describe expected and actual results]
I am expecting .onAppear
to be called once but .onAppear
is called twice.
EDIT: I Removed the link to the project and added a much simpler template code showing the issue.
import SwiftUI
struct RootView: View {
@State private var showDestinationView = false
var body: some View {
NavigationView {
LeftView()
NavigationView {
NavigationLink(isActive: $showDestinationView) {
DestinationView()
} label: {
Button("Show Destination") {
print("didPressButton")
showDestinationView = true
}
}
}
.navigationBarTitle("Title")
.navigationViewStyle(.stack) // Without this line, onAppear is called once.
}
}
}
struct DestinationView: View {
var body: some View {
Text("Destination View")
.onAppear {
print("onAppear")
}
}
}
struct LeftView: View {
var body: some View {
VStack {
Spacer()
Text("Left")
Spacer()
}
}
}
And here are the logs:
didPressButton
onAppear
onAppear