Predict the output
After the first render, the user taps Add 3 times. How many more times does 'Footer body' print?
struct Parent: View {
@State private var count = 0
var body: some View {
VStack {
Text("\(count)")
Footer()
Button("Add") { count += 1 }
}
}
}
struct Footer: View {
var body: some View {
let _ = print("Footer body")
Text("Footer")
}
}Hint 1 of 1
What changed in Footer between renders?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: B 0, because Footer has no inputs that changed
Correct. SwiftUI compares the new Footer value with the old one and skips body when they are equal.
Why not the others?
- A 3, because the parent's body runs again each time. The parent's body runs, but Footer's inputs didn't change, so SwiftUI skips its body.
- C 1. There is no extra render; nothing Footer depends on changed.
- D 6. Each tap causes at most one parent update, and Footer is skipped.