4

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?

Tiago Peres França
  • 3,056
  • 2
  • 21
  • 23
alpennec
  • 1,864
  • 3
  • 18
  • 25
  • I would like to see some answers to this as I have a similar issue though my code is a little different. – gone Nov 13 '21 at 06:30

0 Answers0