SwiftUI: Modifying state during view update, this will cause undefined behavior
A @State/@Published value was changed while SwiftUI was computing a view’s body.
Seen on:
iOS
Meaning
Changing state inside body, inside a computed property used by body, or synchronously in a binding getter causes this runtime warning and unpredictable UI. Move mutations to actions, .onAppear, .task or .onChange.
Common causes
- Assigning state inside var body
- Mutating in a computed property or Binding get
- Calling a method that changes state during layout (GeometryReader)
⚡ Quick fix
- Move the change into .onAppear/.task/.onChange
- Derive values instead of storing them
- Defer with Task { @MainActor in … } as a last resort
Detailed fix by platform
Swift
- swift
var body: some View { Text(title) .onChange(of: size) { _, newValue in layoutWidth = newValue.width } }
How to diagnose
- Location — Which state changes during body?
- Trigger — GeometryReader/computed property?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
Most viewed in iOS
Other ways to find it
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 7 Oct 2026