0Pricing
Swift Academy · Lesson

Equatable Views and @State Scope

Narrow state to limit invalidation.

Equatable Views and @State Scope is a free Swift Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

EquatableView

You can also wrap any Equatable view with .equatable() to request comparison-based skipping without changing the type’s call sites everywhere.

Badge(count: total)
    .equatable()

When Equatable Helps

Equatable shines for views with cheap-to-compare inputs but expensive bodies. The comparison cost must be far less than the render cost to pay off.

The Comparison Cost Trap

If your == is itself expensive (deep comparison of large arrays), it can cost more than the render you are trying to skip. Compare only cheap, decisive fields.

static func == (a: Self, b: Self) -> Bool {
    a.id == b.id && a.version == b.version // cheap fields
}

Default Synthesized Equatable

If all stored properties are Equatable, Swift can synthesize ==. But closures and some types are not Equatable, so you may need a custom implementation that ignores them.

struct Card: View, Equatable {
    let title: String
    let onTap: () -> Void // not Equatable
    static func == (a: Card, b: Card) -> Bool { a.title == b.title }
    var body: some View { Text(title) }
}

Narrowing @State Scope

State placed too high re-renders everything below it. Push @State down to the smallest view that owns it, so changes affect the least UI.

// Move toggle state into the row that uses it
struct Row: View {
    @State private var expanded = false
    var body: some View { /* only this row re-renders */ Text("Row") }
}

Lifting State Only When Shared

Lift state up only when multiple views truly need it. Otherwise local state keeps re-render blast radius small.

Extract Subviews to Localize Updates

Extracting a frequently changing piece into its own view confines re-evaluation to that view, leaving siblings untouched.

struct LiveClock: View {
    @State private var now = Date()
    var body: some View { Text(now, style: .time) }
}

Bindings and Scope

A @Binding connects a child to a parent’s state. Passing a narrow binding (one field) instead of the whole object limits which changes propagate.

struct NameField: View {
    @Binding var name: String // narrow binding
    var body: some View { TextField("Name", text: $name) }
}

Combine Equatable with Scope

The strongest results come from combining techniques: narrow state placement to reduce triggers, plus Equatable to skip the renders that still fire.

Quick Check: Equatable Views

Test your understanding of Equatable views and state scope.

Recap: Equatable Views and State Scope

Making a view Equatable (or using .equatable()) lets SwiftUI skip unchanged renders—worthwhile when comparison is far cheaper than rendering, using only decisive fields.

Narrow @State to the smallest owning view, pass narrow bindings, and extract volatile pieces into their own views. Combining tight state scope with Equatable yields the fewest, cheapest re-renders.

Frequently asked questions

Is the “Equatable Views and @State Scope” lesson free?

Yes — the full text of “Equatable Views and @State Scope” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Equatable Views and @State Scope”?

Narrow state to limit invalidation. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Equatable Views and @State Scope” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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