SwiftUI Data Flow & Composition

State, Binding, Observable models and the Environment; view identity; NavigationStack; and composing reusable views.

Intermediate · about 10 hours · difficulty 3/5

Learn first: iOS App Lifecycle, Xcode Projects & Capabilities, App-wide State Tools

SwiftUI views are lightweight value types recreated often. Data flow tools tell SwiftUI what owns the truth and when to re-render.

Property wrappers and observation

  • @State: private, view-owned value state.
  • @Binding: a two-way reference to state owned by a parent, used for reusable inputs.
  • @Observable classes (Observation framework): shared model objects; views update when the properties they read change. Own them with @State and pass them down or put them in the @Environment.
  • @Environment: read system values (colour scheme, dismiss) or your injected models.

Identity and lists

ForEach and List need stable Identifiable IDs. Changing a view's identity (for example with .id() or conditional branches) resets its state.

Navigation

NavigationStack with .navigationDestination(for:) and a path array enables programmatic and deep-link navigation. Sheets use .sheet(item:).

Composition tips

  • Extract subviews early; small views are faster to diff and easier to preview.
  • Create custom ViewModifiers and styles for consistent design.
  • Use .task to start async work tied to a view's lifetime; it cancels automatically.

Good enough to move on: you can build a list-detail-edit flow where edits propagate through a shared Observable model.

Key concepts

  • @State and @Binding
  • @Observable models
  • Environment injection
  • View identity and Identifiable
  • NavigationStack and typed paths
  • The .task modifier
  • Custom ViewModifiers and styles
  • Previews with sample data

Mini project

Build a recipe manager with list, detail and edit screens sharing one Observable store, plus programmatic navigation to a newly created recipe.

Check yourself

When should a child view take a @Binding instead of its own @State?

Free resources