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
@Statemarks a variable as a source of truth for the view — when it changes, SwiftUI automatically re-renders the affected parts of the UI.@Bindinglets a child view read and write a@Statevariable 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
Buttonin aVStackbefore extracting the subview, otherwise Xcode will not extract it correctly.