SwiftUI's @State
reuses same data?
(ContentView) switch user then, (SubView) SubView should reset dates([Date]), But dates has previous data.
struct ContentView: View {
let users: [String]
@State var selectedUser: String
init() {
self.users = ["User1", "User2", "User3"]
self.selectedUser = users.first!
}
var body: some View {
VStack {
Text(selectedUser)
Picker("User", selection: $selectedUser) {
ForEach(users, id: \.self) { user in
Text(user)
.tag(user)
}
}
.pickerStyle(.segmented)
SubView(user: selectedUser)
}
}
}
struct SubView: View {
let user: String
@State var dates: [Date] = []
var body: some View {
List {
Section(user) {
Button("Add") {
dates.append(.now)
}
ForEach(dates, id: \.self) { date in
Text(date, format: .iso8601)
}
}
}
}
}