Equatable Views and @State Scope
Narrow state to limit invalidation.
Equatable Views and @State Scope is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.
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.
Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “Equatable Views and @State Scope” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Equatable Views and @State Scope”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
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
- Understanding View Identity
- Minimizing Body Re-evaluation
- Equatable Views and @State Scope
- Profiling with the SwiftUI Instrument