History
Loading...
Loading...
August 27, 2025
LazyVStack and LazyHStack instead of regular VStack and HStack when dealing with large datasets. They only render views that are currently visible on screen, dramatically improving scroll performance and memory usage.Always provide explicit identifiers for list items using the `id` parameter and avoid using index-based identification. This ensures SwiftUI can properly track view identity across data changes.
struct ContentView: View {
let items = Array(1...1000)
var body: some View {
List(items, id: \.self) { item in
ItemRow(value: item)
.id(item)
}
}
}
struct ItemRow: View {
let value: Int
var body: some View {
HStack {
Text("Item \(value)")
Spacer()
Text("Value")
.foregroundColor(.secondary)
}
.padding(.vertical, 2)
}
}Proper view identification prevents unnecessary view recreation, maintains scroll position during updates, and enables smooth animations when items are added, removed, or reordered. Without stable IDs, SwiftUI may rebuild entire view hierarchies unnecessarily.