Several other developers and I have been hit by this:
we've filed multiple feedbacks (FB8305409, FB8240609, FB8176223, and more) and would love to know of a solution for this 🙏🏻
Post
Replies
Boosts
Views
Activity
You'll need to use an inert view modifier.
Something along the lines of:
var body: some View {
CatRecorder()
.carStyle(dayTime ? .nightTime : .noStyle)
}
This way SwiftUI knows that this is the same view where the style might change.
With this declaration, the view identity is preserved, but its value (in this case the style value) might change over time
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