SwiftUI - View with a VStack initialized once but the body is called twice

222 views Asked by At

Why, in SwiftUI, the body gets called twice when we use a VStack?

Example without a VStack:

struct ContentView: View {
  var body: some View {
    Text("Hello World")
  }
}

By placing a breaking point at Text("Hello World"), I can see the function body runs a single time, as I expected.

If I slightly change this code by including a VStack, the situation changes:

struct ContentView: View {
  var body: some View {
    VStack { Text("Hello World") }
  }
}

Now, if I place the breakpoint at the same line, I can see the function body is called twice.

Why exactly does this happen? Why just by the inclusion of a VStack SwiftUI needs to run body again?

0

There are 0 answers