Conditionals and Identity

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)
}
...
}

The latter is preferred as it's one view with an inter modifier applied to it.

The first example, the one with if/else branches, has two separate views (with different entities, one for each branch).

Moreover, if the view DogTreatView in the first example also had its own state (via @State or @StateObject), then this state would reset every time you switch branches (a.k.a. the isExpired condition changes).

Meanwhile, in the second example, the state would persist whenever isExpired changes, as in that case, the view is the same, just with a different opacity value

One reason where you might prefer the if-else version is if you wanted a transition between the two states.

Conditionals and Identity
 
 
Q