Equatable Views and @State Scope
Narrow state to limit invalidation.
Equality Can Skip Work
If SwiftUI can prove a view’s inputs are unchanged, it can skip re-rendering it. Making a view Equatable gives SwiftUI a precise way to decide.
Conforming to Equatable
Conform your view to Equatable and SwiftUI compares old and new values. If they are equal, it skips the body.
struct Badge: View, Equatable {
let count: Int
var body: some View { Text("\(count)") }
static func == (a: Badge, b: Badge) -> Bool {
a.count == b.count
}
}All lessons in this course
- Understanding View Identity
- Minimizing Body Re-evaluation
- Equatable Views and @State Scope
- Profiling with the SwiftUI Instrument