[Solved] SwiftUI navigation link and @State have problem in “onAppear()”

Chr1s78 Asks: SwiftUI navigation link and @State have problem in “onAppear()”
I have a view stack ,such as root -> subViewA -> subViewB,and I pass a @State var to each other view ,this var is control something show or hide,this is my code:

Code:
struct RootView: View {
    
    @State var show: Bool = true
    
    var body: some View {
        ZStack(alignment: Alignment(horizontal: .center, vertical: .bottom)) {
            NavigationView {
                NavigationLink(
                    destination: SubViewA(show: $show),
                    label: {
                        Text("to Sub View A")
                            .onAppear {
                                show = true
                                print("Conntent View appear")
                            }
                    })
            }
            
            RoundedRectangle(cornerRadius: 20)
                .frame(width: 400, height: 70)
                .foregroundColor(.blue)
                .offset(y: show ? 0 : 500)
        }
    }
}

Code:
struct SubViewA: View {
    
    @Binding var show: Bool
    
    var body: some View {
        NavigationLink(
            destination: SubViewB(show: $show),
            label: {
                Text("to Sub View B")
                    .onAppear {
                        show = true
                        print("sub View A appear")
                    }
            })
    }
}

Code:
struct SubViewB: View {
    
    @Binding var show: Bool
    
    var body: some View {
        Text("Sub View B")
            .onAppear {
                show = false
                print("Sub View B appear")
            }
    }
}

the console output is :

Code:
Conntent View appear
sub View A appear
2021-08-02 15:22:58.284604+0800 NavigationLink[32349:489749] [Assert] displayModeButtonItem is internally managed and not exposed for DoubleColumn style. Returning an empty, disconnected UIBarButtonItem to fulfill the non-null contract.
Sub View B appear
Conntent View appear

when navigate to the subViewB,the output is “Sub View B appear” and “Content View appear”,why it print the rootView’s onappear() info?how can I fixed it?

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.