SwiftUI's @State reuses same data?

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)
        }
      }
    }
  }
}

The state of the view is saved, as a State.

You should use a Binding:

struct ContentView: View {
    let users: [String]
    @State var selectedUser: String
    @State var dates: [Date] = []
    
    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, dates: $dates)
        }
    }
}

struct SubView: View {
    let user: String
//    @State var dates: [Date] = []
    @Binding 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)
                }
            }
        }
    }
}

If you want to keep the state of each user, change as follows:

struct ContentView: View {
    let users: [String]
    @State var selectedUser: String
    @State var datesOfUsers: [String: [Date]]

    init() {
        self.users = ["User1", "User2", "User3"]
        self.selectedUser = users.first!
        self.datesOfUsers = [:]
        self.datesOfUsers["User1"] = []
        self.datesOfUsers["User2"] = []
        self.datesOfUsers["User3"] = []
    }
    
    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, usersDates: $datesOfUsers)
        }
    }
}

struct SubView: View {
    let user: String
    var dates: [Date] {
        usersDates[user] ?? []
    }
    @Binding var usersDates: [String: [Date]]

    var body: some View {
        List {
            Section(user) {
                Button("Add") {
                    if usersDates[user] == nil {
                        usersDates[user] = [.now]
                    } else {
                        usersDates[user]!.append(.now)
                    }
                }
                
                ForEach(dates, id: \.self) { date in
                    Text(date, format: .iso8601)
                }
            }
        }
    }
}
SwiftUI's @State reuses same data?
 
 
Q