I want to init a navigation stack in SwiftUI. Thus I want to push a second view immediately after a first view appears. Approach: Using a @State var, which is set to true in onAppear, and thus activates a NavigationLink in first view.
However, the approach is not working. The push is done and then the second view is immediately dismissed. The expected behaviour is that the second screen stays on screen.
This is a simplified demo:
struct SecondView: View {
	var body: some View {
		Text("Second")
			.navigationTitle("Second")
			.onAppear(perform: {
				print("Second: onAppear")
			})
	}
}
struct FirstView: View {
	 @State var linkActive = false
	 var body: some View {
		 NavigationLink(destination: SecondView(), isActive: $linkActive) {
			 Text("Goto second")
		 }
		 .navigationTitle("First")
		 .onAppear(perform: {
			 print("First: onAppear")
			 linkActive = true
		 })
		 .onChange(of: linkActive) { value in
			 print("First: linkActive changed to: \(linkActive)")
		 }
	 }
}
struct SwiftUIView: View {
	 var body: some View {
		 NavigationView {
			 NavigationLink(destination: FirstView()) {
				 Text("Goto first")
			 }
		 }
		 .navigationViewStyle(StackNavigationViewStyle())
	 }
}
Log output:
First: onAppear
First: linkActive changed to: true
Second: onAppear
First: linkActive changed to: false
Trying to figure out if this is a SwiftUI bug, or something I’m doing wrong. May someone help?