title: SwiftUI how to keep track of page index in PageTabViewStyle?
body: <p>I'm trying to keep track of what page the user is on in a <code>TabView</code> that is <code>PageTabViewStyle</code> in SwiftUI but I can't figure out the best way to keep track of the page index?  Using <code>.onAppear</code> doesn't work well as it gets called multiple times and pages 2 and 3 get called even when not on the screen. </p>
  <pre><code>@State var pageIndex = 0
  
      var body: some View {
      VStack {
          Text(&quot;current page = \(0) &quot;)
          TabView {
              Text(&quot;First&quot;)
                  .onAppear {
                      pageIndex = 0
                  }
              Text(&quot;Second&quot;)
              .onAppear {
                      pageIndex = 1
                  }
              Text(&quot;Third&quot;)
              .onAppear {
                      pageIndex = 2
                  }
          }
           .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
           }
      }
  }
  </code></pre>