I tested the sample code, even using a random color to debug the view to check if it is being recreated, it looks like VStack in List = unstable identity when presentation state changes.
I applied .border() modifier with the random color, to track if a view is recreated. Only when the presentation state changes, then the count is reset.
Color(
red: .random(in: 0...1),
green: .random(in: 0...1),
blue: .random(in: 0...1)
)
Separate the buttons to test, and when the isPresented flag changes after the alert, the LibraryView in the VStack causes issues.
private struct ViewWithAlert: View {
@State private var isPresented: Bool = false
@State private var presentedCount = 0
var body: some View {
VStack {
// Test without alert - just increment the counter
Button {
// isPresented = true // Comment out alert
presentedCount += 1
} label: {
Text("Increment count: \(presentedCount)")
.border(Color.random)
}
// Separate button to show alert // -> When tapped, presentedCount resets to 0 on tap OK
Button("Show Alert") {
isPresented = true
}
}
.alert("Hello", isPresented: self.$isPresented) {
Button("OK") { }
}
}
}