existential-birds/beagle / plugins/beagle-ios/skills/swiftui-code-review
swiftui-code-review
VerifiedReviews SwiftUI code for view composition, state management, performance, and accessibility. Use when reviewing .swift files containing SwiftUI views, property wrappers (@State, @Binding, @Observable), or UI code.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 2.8 KB |
| references/accessibility.md | markdown | 3.3 KB |
| references/performance.md | markdown | 3.0 KB |
| references/state-management.md | markdown | 2.7 KB |
| references/view-composition.md | markdown | 2.2 KB |
references/state-management.md
View on GitHub# State Management
## Property Wrapper Quick Reference
| iOS Version | View Creates Object | View Receives Object | Two-Way Binding |
|-------------|---------------------|---------------------|-----------------|
| < iOS 17 | `@StateObject` | `@ObservedObject` | `@Binding` |
| iOS 17+ | `@State` (for @Observable) | Plain property | `@Bindable` |
## @StateObject vs @ObservedObject
Use @StateObject when the view creates and owns the object. Use @ObservedObject when passed from a parent.
```swift
// BAD - recreated on every view rebuild
struct ContentView: View {
@ObservedObject var viewModel = ViewModel() // Wrong!
}
// GOOD - survives view rebuilds
struct ContentView: View {
@StateObject var viewModel = ViewModel()
}
// GOOD - received from parent
struct ChildView: View {
@ObservedObject var viewModel: ViewModel // Not creating it
}
```
## iOS 17+ @Observable Pattern
With @Observable, use @State at app level, plain properties in children.
```swift
@Observable class AppStore { /* ... */ }
@main
struct MyApp: App {
@State private var store = AppStore()
var body: some Scene {
WindowGroup { ContentView(store: store) }
}
}
// Child receives without wrapper
struct ChildView: View {
var store: AppStore // Read-only access
}
// For two-way bindings, use @Bindable
struct EditView: View {
@Bindable var user: User
var body: some View {
TextField("Name", text: $user.name)
}
}
```
## @State Mistakes
```swift
// BAD - @State ignores external updates
struct ChildView: View {
@State var user: User // Updates from parent ignored!
}
// BAD - property observers don't work
@State var count = 0 {
didSet { print("Changed") } // Never called!
}
// GOOD - use .onChange modifier
.onChange(of: count) { oldValue, newValue in
print("Changed from \(oldValue) to \(newValue)")
}
```
## Environment Usage (iOS 17+)
```swift
// Old syntax
@Environment(\.modelContext) private var context
// New syntax for custom types
@Environment(AuthService.self) private var authService
```
## Critical Anti-Patterns
| Pattern | Issue |
|---------|-------|
| `@ObservedObject var vm = ViewModel()` | Creates new object on rebuild |
| `@State var model: SomeClass` | Reference types need @StateObject |
| `@State` in child for passed data | Ignores parent updates |
| didSet on @State | Property observers don't fire |
| Missing @Bindable for bindings | Can't bind to @Observable properties |
## Review Questions
1. Is @StateObject used when the view creates the object?
2. Is @ObservedObject only used for objects passed from parent?
3. For iOS 17+, is @State with @Observable in a stable parent view?
4. Is @Bindable used where two-way bindings are needed?
5. Is .onChange used instead of didSet on @State?
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation). | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK). | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |