NavigationStack doesn't refresh its body when a state value changes from `nil`

I'm trying to migrate my app from using NavigationView to NavigationStack and have run into an issue. It seems that when a View has a state variable with an optional type that's initialised as nil, then asynchronously changes to something other than nil, the view doesn't update.

If I use NavigationView it works fine, and if I initialise the state variable with something other than nil it also works fine.

I can't figure out why – any advice is appreciated. This is on iOS 6 beta 3.

struct ContentView: View {    

  // Initialise this as something other than nil and it'll work
  // e.g. @State private var foo: String? = ""
  @State private var foo: String? = nil

  var body: some View {        

    // Change this to NavigationView and it'll work.    
    NavigationStack {     
      if let foo { Text(foo) }     

      // ProgressView never goes away 
      else { ProgressView() }   
     }    
    .task {     
      // Simulate loading some data asynchronously.
      DispatchQueue.main.asyncAfter(deadline: .now() + 1) {        
        foo = "loaded"      
      }    
    }  
  }
}

View as a Gist on GitHub

Answered by BabyJ in 721353022

From the iOS & iPadOS 16 Beta 3 Release Notes:

Conditional views in columns of NavigationSplitView fail to update on some state changes. (91311311)

Workaround: Wrap the contents of the column in a ZStack.

It mentions NavigationSplitView, but it must apply to NavigationStack as wrapping the if statement in your example in a ZStack updates the view.

Accepted Answer

From the iOS & iPadOS 16 Beta 3 Release Notes:

Conditional views in columns of NavigationSplitView fail to update on some state changes. (91311311)

Workaround: Wrap the contents of the column in a ZStack.

It mentions NavigationSplitView, but it must apply to NavigationStack as wrapping the if statement in your example in a ZStack updates the view.

I just tried this and it did indeed fix the problem. Thanks for helping!

Just to close the loop on this, it's no longer an issue from iOS 16 beta 5 onwards.

NavigationStack doesn't refresh its body when a state value changes from `nil`
 
 
Q