I have a List with rows which push a View. That view has another List which pushing another View. The original List, and the first pushed List will update when the data chan
This is the test I made and is working well, everything updates as expected.
struct User: Identifiable {
var id: String
var name: String
var items: [Item]
}
struct Item: Identifiable {
var id: String
var name: String
var details: [String]
}
class App: ObservableObject {
@Published var users = [User]()
init() {
let items1 = [Item(id: UUID().uuidString, name: "item1", details: ["d1","d2"]), Item(id: UUID().uuidString, name: "item2", details: ["d3","d4"])]
let items2 = [Item(id: UUID().uuidString, name: "item3", details: ["e1","e2"]), Item(id: UUID().uuidString, name: "item4", details: ["e3","e4"])]
users.append(User(id: UUID().uuidString, name: "user1", items: items1))
users.append(User(id: UUID().uuidString, name: "user2", items: items2))
}
}
struct ContentView: View {
@ObservedObject var app = App()
var body: some View {
NavigationView {
List {
ForEach(app.users) { user in
NavigationLink(destination: UserView(user: user)) {
Text(user.name)
}
}
}
}
}
}
struct UserView: View {
@State var user: User
var body: some View {
List {
ForEach(user.items) { item in
NavigationLink(destination: ItemView(item: item)) {
Text(item.name)
}
}
}
}
}
struct ItemView: View {
@State var item: Item
var body: some View {
List {
ForEach(item.details, id: \.self) { detail in
Text(detail)
}
}
}
}