Understanding State and Binding (Chapter 7)

Overview

This short lesson introduces “state management” by showing how to change the appearance of buttons based on user actions. Tap a button for example, and the color and/or size of the button can be made to vary based on the fact that it was tapped.

When asked to extract a subview from the button, make sure you wrap the button in a VStack and then extract it.

Read Chapter 7: Understanding State and Binding

Key Takeaways

  • @State marks a variable as a source of truth for the view — when it changes, SwiftUI automatically re-renders the affected parts of the UI.
  • @Binding lets a child view read and write a @State variable owned by a parent, enabling two-way data flow between views.
  • You never manually refresh the UI in SwiftUI — state changes trigger re-renders automatically.
  • Practical tip: wrap your Button in a VStack before extracting the subview, otherwise Xcode will not extract it correctly.