title: Need help regarding "pop to root" view implementation of a NavigationView in SwiftUI using a List container
body: <p>I'm trying to implement a List of NavigationLinks that will allow me to navigate between views of up to 3 levels of navigation links.</p>
  <p>Main View -&gt; View2 -&gt; View3 -&gt; View4</p>
  <p>When I reach View4, the view will not have the default &quot;back&quot; button at the navigation bar but will instead have a button that will allow me to navigate back to &quot;Main View&quot;</p>
  <p>Using the solution mentioned <a href="https://stackoverflow.com/questions/57334455/swiftui-how-to-pop-to-root-view">here</a>, I was able to (sort of) achieve what I wanted by making slight modifications in the solution so that the isActive bit is implemented as an array of isActive bits each corresponding to the number of navigation links that is iterated within the List container.</p>
  <p>The only problem that I noticed though is that the pop-to-root view operation using the isActive bits will only work if I use it with the @State property wrapper but will do nothing if my isActive bits are contained within an ObservableObject class instance. My issue with using the @State wrapper is that I will have to pre-define a fixed array size and populate it with &quot;false&quot; bits which is not what I want since the number of items that I can iterate within the List container may vary so I want the isActive array size to be the same as the number of items that I'm iterating within the List container.</p>
  <p>Here's the simplified implementation of the pop-to-root NavigationView code for reference. The top half of the NavigationLinks are the ones that use the fixed-size isActive bit array with the @State wrapper which works while the bottom half of the NavigationLinks are the ones that use the observable object which can have a flexible isActive bit array size but I can't seem to figure out why this approach won't work.</p>
  <p>Main View:</p>
  <pre><code>struct ContentView: View {
      @ObservedObject var itemGroup: ItemGroup
      // If I use @State wrapper, I need to pre allocate a fixed array size and populate
      // it with 'false' values. 
      @State var isActive: [Bool] = [false, false, false, false]
      
      var body: some View {
          NavigationView {
              VStack {
                  
              // MARK: Navigation links using isActive property with @State wrapper 
              // (this implementation works but the isActive array size needs to be defined
              // with a fixed size, I would prefer the size to be variable)
              List(1..&lt;5) { item in
                  NavigationLink(destination: View2(isActive: $isActive[item-1], sub: &quot;sub\(item)&quot;), isActive: $isActive[item-1]) {
                      Text(&quot;Go to view 2 using @State, sub\(item)&quot;)
                          .padding()
                          .background(Color.blue)
                          .foregroundColor(.white)
                  }
              }
              
              // MARK: Navigation links using isActive property defined within an observable 
              // class object 
              // (I would have preferred this method since I can have the
              // flexibility of allocating an isActive array based on the number of
              // NavigationLink views generated by the list container but the pop-to-root
              // operation won't work with this approach)
              //  Note: The range 1..&lt;5 is used here for illustration purposes only but is actually an array of objects whose array size may vary
              List(1..&lt;5) { item in
                  NavigationLink(destination: View2(isActive: $itemGroup.isActive[item-1], sub: &quot;sub\(item)&quot;), isActive: $itemGroup.isActive[item-1]) {
                      Text(&quot;Go to view 2, sub\(item)&quot;)
                          .padding()
                          .background(Color.blue)
                          .foregroundColor(.white)
                  }
              }
              .navigationBarTitle(&quot;Main view&quot;)
              }   
          }   
      }
  }
  
  class ItemGroup: ObservableObject {
      @Published var isActive: [Bool] = []
      
      init() {
          //  Note: The range 1..&lt;5 is used only for illustration purposes only.
          //        In my use case, this is actually an array of objects whose array size may vary.
          for _ in 1..&lt;5 {
              isActive.append(false)
          }
      }
  }
  </code></pre>
  <p>View2:</p>
  <pre><code>struct View2: View {
      @Binding var isActive: Bool
      @State var sub: String
      var body: some View {
          NavigationLink(destination: View3(isActive: $isActive)) {
              Text(&quot;Go to view 3&quot;)
                  .padding()
                  .background(Color.blue)
                  .foregroundColor(.white)
          }
          .navigationBarTitle(&quot;View 2, \(sub)&quot;)
      }
  }
  </code></pre>
  <p>View3:</p>
  <pre><code>struct View3: View {
      @Binding var isActive: Bool
      var body: some View {
          NavigationLink(destination: View4(isActive: $isActive)) {
              Text(&quot;Go to view 4&quot;)
                  .padding()
                  .background(Color.blue)
                  .foregroundColor(.white)
          }
          .navigationBarTitle(&quot;View 3&quot;)
      }
  }
  </code></pre>
  <p>View4 (this is the one with the pop-to-root view function):</p>
  <pre><code>struct View4: View {
      @Binding var isActive: Bool
      
      var body: some View {
          VStack {
              Text(&quot;View 4&quot;)
              Button(&quot;Back to Main View&quot;, action: {
                  isActive = false
              })
              .padding()
              .background(Color.blue)
              .foregroundColor(.white)
          }
          .navigationBarHidden(true)
      }
  }
  </code></pre>
  <p>Here are the gif demos of the two different implementations.</p>
  <p><a href="https://i.stack.imgur.com/OKcR6.gif" rel="nofollow noreferrer">Navigation using isActive array with @State property wrapper (Click link)</a></p>
  <p><a href="https://i.stack.imgur.com/9trL1.gif" rel="nofollow noreferrer">Navigation using isActive array within an observable object (Click link)</a></p>
  <p>Can anyone please help me figure out why the bottom half of the NavigationLinks won't work and what changes should I do to make it work?</p>
  <p>Appreciate the help. Thanks.</p>