Modifying state during view update 🍎 iOS

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

  1. Move the change into .onAppear/.task/.onChange
  2. Derive values instead of storing them
  3. Defer with Task { @MainActor in … } as a last resort

Detailed fix by platform

Swift

  1. swift
    var body: some View {
        Text(title)
            .onChange(of: size) { _, newValue in layoutWidth = newValue.width }
    }

How to diagnose

  1. Location — Which state changes during body?
  2. 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:

🧠 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.