0Pricing
Swift Academy · Lesson

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

  1. Understanding View Identity
  2. Minimizing Body Re-evaluation
  3. Equatable Views and @State Scope
  4. Profiling with the SwiftUI Instrument
← Back to Swift Academy