How it works
EquatableView is a view that wraps another view and uses that view's Equatable conformance to decide whether it actually needs to be re-rendered. Normally SwiftUI re-evaluates a view's body whenever its enclosing state changes; by comparing the old and new values of the wrapped view, EquatableView lets SwiftUI skip that work when the inputs are equal. Reach for it as a performance optimization around a view whose body is expensive to recompute and whose identity is fully captured by its stored properties.
Conform the wrapped view to Equatable
EquatableViewcan only short-circuit a re-render if it can compare two instances of its content, so the wrapped view must conform toEquatable. In the example,Badgeis declaredstruct Badge: View, Equatable, and because its only stored property islet count: Int, the compiler synthesizes an==that compares badges purely by theircount.Wrap the view with the content initializer
You create the wrapper by passing the view you want to guard into
EquatableView(content:). HereEquatableView(content: Badge(count: 3))hands aBadgeinstance to the wrapper, which now stands in for that badge in the view tree and takes over the decision of when to re-evaluate itsbody.Let equality gate the re-render
On each update,
EquatableViewcompares the new content value against the previous one using the synthesized==. If they are equal it reuses the existing rendering and does not re-run the wrappedbody; only an unequal value triggers a fresh evaluation ofBadge.body— theLabel,padding, andbackgroundthat make up the badge.Prefer the equatable() modifier in practice
EquatableViewis most often applied through theequatable()modifier rather than constructed by hand:Badge(count: 3).equatable()produces the sameEquatableViewwrapper. Spelling it out asEquatableView(content:), as the example does, makes the wrapping explicit while behaving identically.
Badge(count: 3) to a value driven by @State and toggle it between two distinct numbers versus repeatedly re-applying the same number — EquatableView re-renders only when count actually changes.Example & preview
Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.
struct EquatableViewDemo: View {
struct Badge: View, Equatable {
let count: Int
var body: some View {
Label("\(count) items", systemImage: "shippingbox.fill")
.padding(8)
.background(.blue.opacity(0.15), in: Capsule())
}
}
var body: some View {
VStack(spacing: 12) {
Text("EquatableView")
.font(.headline)
EquatableView(content: Badge(count: 3))
Text("Re-renders only when count changes")
.font(.caption)
.foregroundStyle(.secondary)
}
.padding()
}
}