Swift UI TabView selection binding strange behaviour ?

Hello, can someone please explain to me how does SwiftUI TabView works "under the hood" , I don't understand why do all views in TabView get reinitialized each time I switch between tabs. Xcode Version 15.3, iOS 16+

Below is the code snippet :

struct ScreenOne: View {
    init() {
        print("ScreenOne init called !")
    }

    var body: some View {
        Text("This is screen one!")
    }
}

struct ScreenTwo: View {
    init() {
        print("ScreenTwo init called !")
    }
    var body: some View {
        Text("This is screen two !")
    }
}

struct TabViewTest: View {
    @State var selectedIndex: Int = 1
    var body: some View {
            TabView(selection: $selectedIndex) {
                ScreenOne()
                    .tag(1)
                    .tabItem {
                        Text("Item 1")
                    }
                ScreenTwo()
                    .tag(2)
                    .tabItem {
                        Text("Item 2")
                    }
            }
//            .onChange(of: selectedIndex) { oldValue, newValue in
//
//            }

        // NOTE: When code above is uncommented
        // Screen one & Screen two initializers get called
        // each time switch to different tab occurs
    }
}

Snippet output with the commented out code :

  1. App loads
  2. Both print statements get called -> "ScreenOne init called ! & "ScreenTwo init called !
  3. Switch between taps
  4. Nothing happens

Snippet output with the uncommented code :

  1. App loads
  2. Both print statements get called -> ScreenOne init called ! & ScreenTwo init called !
  3. Switch between taps
  4. Both print statements get called -> ScreenOne init called ! & ScreenTwo init called !

@eskimo heeelp :) Thanks in advance !

Not an explanation (I am interested to read an "authorised" analysis, but my understanding is that calling onChange on the State var, leads TabView to reevaluate its content. Why ? That is the question !

A possible reason: as oldValue, newValue change, that forces the TabView to redraw (and reevaluate).

Generally, I have noticed it is very hard (and hazardous) to try to guess how SwiftUI works to reevaluate views…

Swift UI TabView selection binding strange behaviour ?
 
 
Q