title: I'm trying to implement a view stack in swiftui and my @State objects are being reset for reasons that are unclear to me
body: <p>I'm new to swiftui and doing an experiment with pushing and popping views with a stack. When I pop a view off the stack, the @State variable of the prior view has been reset and I don't understand why.</p>
  <p>This demo code was tested on macos.</p>
  <pre><code>import SwiftUI
  
  typealias Push = (AnyView) -&gt; ()
  typealias Pop = () -&gt; ()
  
  struct PushKey: EnvironmentKey {
      
      static let defaultValue: Push = { _ in }
      
  }
  
  struct PopKey: EnvironmentKey {
      
      static let defaultValue: Pop = {() in }
      
  }
  
  extension EnvironmentValues {
      
      var push: Push {
          get { self[PushKey.self] }
          set { self[PushKey.self] = newValue }
      }
      
      var pop: Pop {
          get { self[PopKey.self] }
          set { self[PopKey.self] = newValue }
      }
  }
  
  struct ContentView: View {
      @State private var stack: [AnyView]
  
      var body: some View {
          currentView()
              .environment(\.push, push)
              .environment(\.pop, pop)
              .frame(width: 600.0, height: 400.0)
      }
      
      public init() {
          _stack = State(initialValue: [AnyView(AAA())])
      }
      
      private func currentView() -&gt; AnyView {
          if stack.count == 0 {
              return AnyView(Text(&quot;stack empty&quot;))
          }
          return stack.last!
      }
      
      public func push(_ content: AnyView) {
          stack.append(content)
      }
      
      public func pop() {
          stack.removeLast()
      }
  }
  
  struct AAA : View {
      @State private var data = &quot;default text&quot;
      @Environment(\.push) var push
  
      var body: some View {
          VStack {
              TextEditor(text: $data)
              Button(&quot;Push&quot;) {
                  self.push(AnyView(BBB()))
              }
          }
      }
  }
  
  struct BBB : View {
      @Environment(\.pop) var pop
  
      var body: some View {
          VStack {
              Button(&quot;Pop&quot;) {
                  self.pop()
              }
          }
      }
  }
  </code></pre>
  <p>If I type some text into the editor then hit Push, then Pop out of that view, I was expecting the text editor to maintain my changes but it reverts to the default text.</p>
  <p>What am I missing?</p>
  <p>Edit:</p>
  <p>I guess this is really a question of how are NavigationView and NavigationLink implemented.  This simple code does the what I'm trying to do:</p>
  <pre><code>import SwiftUI
  
  struct MyView: View {
      @State var text = &quot;default text&quot;
      var body: some View {
          VStack {
              TextEditor(text: $text)
              NavigationLink(destination: MyView()) {
                  Text(&quot;Push&quot;)
              }
          }
      }
  }
  
  struct ContentView: View {
      var body: some View {
          NavigationView {
              MyView()
          }
      }
  }
  </code></pre>
  <p>run that on iOS so you get a nav stack. edit the text, then push. Edit again if you want, then go back and see state is retained.</p>
  <p>My code is trying to do the same thing in principle.</p>