When using conditionals in view bodies, can I preserve identity between the true
and false
sides of the conditional by adding an explicit id?
struct DogTreat: Identifiable {
var expirationDate: Date
var serialID: String
var id: String { serialID }
}
...
struct WrapperView: View {
...
var treat: DogTreat
var isExpired: Bool { treat.expirationDate < .now }
var body: some View {
if isExpired {
DogTreatView(treat)
.id(treat.id)
.opacity(0.75)
else {
DogTreatView(treat)
.id(treat.id)
}
}
...
}
Does this perform / behave the same as
struct WrapperView: View {
...
var treat: DogTreat
var isExpired: Bool { treat.expirationDate < .now }
var body: some View {
DogTreatView(treat)
.opacity(isExpired ? 0.75 : 1.0)
}
...
}