History
Loading...
Loading...
August 30, 2025
sheet(item:) instead of sheet(isPresented:) when presenting sheets with dynamic content. This automatically handles the binding state and provides cleaner dismissal behavior.Using `sheet(item:)` with Identifiable models creates a direct relationship between your data and the sheet presentation. The sheet automatically presents when the item is set and dismisses when it becomes nil.
struct User: Identifiable {
let id = UUID()
let name: String
let email: String
}
struct ContentView: View {
@State private var selectedUser: User?
var body: some View {
List(users, id: \.id) { user in
Button(user.name) {
selectedUser = user
}
}
.sheet(item: $selectedUser) { user in
UserDetailView(user: user)
}
}
}This approach eliminates the need for separate boolean flags, reduces state management complexity, and prevents common bugs like forgetting to reset presentation states. It also makes the code more readable by clearly showing what data drives the sheet presentation.