title: Changing state in onAppear breaks PageViewController
body: <p>I'm struggling to understand why a PageViewController does not work as expected when there is a change to a @Published EnvironmentObject variable during <code>.onAppear</code> in its parent View.</p>
  
  <p>If I comment out the <code>.onAppear</code> modifier in <code>DetailView</code>, then everything works fine - the slides swipe as expected of a PageViewController. But as soon as that modifier is back in, then the pages do not swipe correctly.</p>
  
  <p>It's got to be something to do with the re-render due to state change, but I can't figure it out. I know that <code>updateUIViewController</code> is called on the state change, and this seems to change the array of viewControllers.</p>
  
  <p>If anyone has a steer on how to solve this, I'll be really grateful to hear about it!</p>
  
  <p>If you are running the code below, make sure to change your SceneDelegate:</p>
  
  <pre><code>let contentView = ContentView()
  </code></pre>
  
  <p>to</p>
  
  <pre><code>let contentView = ContentView().environmentObject(Global())
  </code></pre>
  
  <p>So here's a simple example of the issue:</p>
  
  <pre class="lang-swift prettyprint-override"><code>import SwiftUI
  import Foundation
  import UIKit
  
  struct Item: Hashable {
    var text: String
  }
  
  let testItems: [Item] = [
    Item(text: "I am item 1"),
    Item(text: "I am item 2"),
    Item(text: "I am item 3")
  ]
  
  final class Global : ObservableObject {
    @Published var hideText: Bool = false
  }
  
  struct ContentView: View {
  
    @EnvironmentObject var global: Global
  
    var body: some View {
  
      NavigationView {
        VStack {
          List(testItems, id: \.self) { item in
            NavigationLink(destination: DetailView(item: item)) {
              Text(item.text)
            }
          }
  
          Text("I should vanish when detail view loads")
            .opacity(self.global.hideText ? 0 : 1)
        }
        .navigationBarTitle("Test")
      }
    }
  }
  
  struct DetailView: View {
  
    @EnvironmentObject var global: Global
  
    var item: Item
    var testViews: [Text] = [
      Text("Slide 1").font(.title),
      Text("Slide 2").font(.title),
      Text("Slide 3").font(.title),
    ]
  
    var body: some View {
  
      // The .onAppear modifier breaks the PageViewController
      // when it is commented out, the slides work as expected
  
      PageView(testViews)
        .onAppear {
          self.global.hideText = true
        }
    }
  }
  
  struct PageView&lt;Page: View&gt;: View {
  
    @State var currentPage = 0
  
    var viewControllers: [UIHostingController&lt;Page&gt;]
  
    init(_ views: [Page]) {
      self.viewControllers = views.map{ UIHostingController(rootView: $0) }
    }
  
    var body: some View {
      VStack(alignment: .center) {
        PageViewController(
          viewControllers: viewControllers,
          currentPageIndex: $currentPage
        )
      }
    }
  }
  
  struct PageViewController: UIViewControllerRepresentable {
  
    var viewControllers: [UIViewController]
  
    @Binding var currentPageIndex: Int
  
    func makeCoordinator() -&gt; Coordinator {
      Coordinator(self)
    }
  
    func makeUIViewController(context: Context) -&gt; UIPageViewController {
      let pageViewController = UIPageViewController(
        transitionStyle: .scroll,
        navigationOrientation: .horizontal
      )
  
      pageViewController.view.backgroundColor = UIColor.clear
      pageViewController.dataSource = context.coordinator
      pageViewController.delegate = context.coordinator
  
      return pageViewController
    }
  
    func updateUIViewController(_ pageViewController: UIPageViewController, context: Context) {
      pageViewController.setViewControllers([viewControllers[currentPageIndex]], direction: .forward, animated: true)
    }
  
    class Coordinator: NSObject, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
  
      var parent: PageViewController
  
      init(_ pageViewController: PageViewController) {
        self.parent = pageViewController
      }
  
      func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -&gt; UIViewController? {
          // retrieves the index of the currently displayed view controller
          guard let index = parent.viewControllers.firstIndex(of: viewController) else {
            return nil
          }
  
          // shows the last view controller when the user swipes back from the first view controller
          if index == 0 {
            return parent.viewControllers.last
          }
  
          //show the view controller before the currently displayed view controller
          return parent.viewControllers[index - 1]
        }
  
        func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -&gt; UIViewController? {
            // retrieves the index of the currently displayed view controller
            guard let index = parent.viewControllers.firstIndex(of: viewController) else {
              print("View controller not found!!")
              return nil
            }
            // shows the first view controller when the user swipes further from the last view controller
            if index + 1 == parent.viewControllers.count {
              return parent.viewControllers.first
            }
  
            // show the view controller after the currently displayed view controller
            return parent.viewControllers[index + 1]
        }
  
        func pageViewController(_ pageViewController: UIPageViewController,
          didFinishAnimating finished: Bool,
          previousViewControllers: [UIViewController],
          transitionCompleted completed: Bool
        ) {
          if completed,
            let visibleViewController = pageViewController.viewControllers?.first,
            let index = parent.viewControllers.firstIndex(of: visibleViewController) {
              parent.currentPageIndex = index
            }
        }
    }
  }
  
  struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
      ContentView()
        .environmentObject(Global())
    }
  }
  </code></pre>