0

[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
  • Please take the [tour](https://stackoverflow.com/tour) and read [How to Ask](https://stackoverflow.com/help/how-to-ask) to improve, edit and format your questions. Without a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) it is impossible to help you troubleshoot. The MINIMAL example code should be included in your question not as a link. – lorem ipsum Jan 07 '22 at 19:12
  • @loremipsum thanks for your feedback. I hope the edition makes the question easier to understand. – Jérémie Pouillon Jan 07 '22 at 19:26
  • Why do you need to nest a second `NavigationView`? You typically only need one. It is possible that having two such views is what might be triggering the double call. – ScottM Jan 07 '22 at 21:59
  • type `onAppear called twice` in the search bar, and you will get many SO answers, like: https://stackoverflow.com/questions/63080830/swifui-onappear-gets-called-twice – workingdog support Ukraine Jan 07 '22 at 23:40
  • @ScottMatthewman I don’t know if using two NavigationViews is the problem here because if you remove the `.navigationViewStyle(.stack)` modifier on the second NavigationView, `.onAppear` is called once. Otherwise, I have information displayed in the navigationBar of the first navigation view that need to be displayed when navigating within the second navigationView. I did a [drawing](https://ibb.co/M69D0J4) showing what I try to achieve, it might be easier to understand. – Jérémie Pouillon Jan 08 '22 at 05:13
  • yeah thanks @workingdog, but since this answer is 6 months old and doesn't mention any use of `.navigationViewStyle`, I prefered ask my question in case someone had experiences the same issue and found a solution or knew why it is happening. But I do have this hacky solution in mind. – Jérémie Pouillon Jan 08 '22 at 05:31

0 Answers0