I have a struct User with a list of hobbies as follows:
struct User: Hashable, Codable, Identifiable, Equatable {
var id: String
var name: String
var hobbies: [String]
}
I then generate a list view using a list of users as a state variable. When the button in the list view is clicked a new hobby is added to a user's hobbies.
struct UsersListView: View {
@State var users: [User]
var body: some View {
TitleView(titleText: "Friends")
.padding(.leading, 30.0)
List(userDisplayList) { user in
Text(user.name)
Text(user.hobbies.joined(separator: ", "))
Button(action: {self.users[0].hobbies.append("new hobby")}
}
}
}
}
}
However, when I click on the button, the users state variable does not change and remains the same. Can anyone explain what is going on here?