title: Add an Array element programminglly an pop up "Accessing State's value outside of being installed on a View." alert
body: <p>I am trying to programminglly add a UI_Element struct to UI_Group array triggered by a socketio listening event, but there is an alert:</p>
  <blockquote>
  <p><em>&quot;Accessing State's value outside of being installed on a View. This will result in a constant Binding of the initial value and will not
  update.&quot;</em></p>
  </blockquote>
  <p>It seems that I shouldn't access a state's value(item.$name) in ContentView struct, but I don't know how to fix it～ＱＱ</p>
  <p>Also, I tried to move the UI_element struct into Service class or ContentView struct, and it couldn't work either.</p>
  <pre><code>final class Service: ObservableObject {
      private var manager = SocketManager(socketURL: URL(string: &quot;http://x.x.x.x&quot;)!, config: [.log(true), .compress])
      
      @Published var messages = [String]()
      @Published var UI_Group = [UI_Element]()
      
      init() {
          let socket = manager.defaultSocket
          socket.on(clientEvent: .connect) {(data, ack) in
              print(&quot;Connected&quot;)
              socket.emit(&quot;Python Server Port&quot;, &quot;Hi Python Server!&quot;)
          }
          
          socket.on(&quot;iOS Client Port&quot;) {[weak self] (data, ack) in
              if let data = data[0] as? [String: String],
                 let rawMessage = data[&quot;msg&quot;] {
                  DispatchQueue.main.async {
                      //self?.messages.append(rawMessage)
                      switch rawMessage {
                      case &quot;0&quot;:
                          print(&quot;新增中&quot;)
                          self?.UI_Group.append(UI_Element())
                          if self?.UI_Group != nil {
                              guard let num_element = self?.UI_Group.count else {
                                  return print(&quot;無法新增&quot;)
                              }
                              print(&quot;已新增\(num_element)個元素&quot;)
                          }
                      default:
                          print(&quot;hello&quot;)
                      }
                  }
              }
          socket.connect()
      }
  }
  
  struct ContentView: View {
          
      @ObservedObject var service = Service()
      
      var body: some View {
          
          VStack{
              ForEach(service.UI_Group, id: \.id){ item in
                  TextField(&quot;Please enter text.&quot;, text: item.$name)
                      .padding()
                      .accessibilityLabel(&quot;請輸入元素名稱&quot;)
                      .textFieldStyle(.roundedBorder)
                      .offset(x: CGFloat(item.center_x), y: CGFloat(item.center_y))
              }
          }
      }
  }
  
  struct UI_Element: Identifiable {
  
      let id = UUID()
      let type:Int
      @State var name = &quot;&quot;
      @State var center_x = 0
      @State var center_y = 0
      @State var ui_state = 0
  
      init() {
          self.type = 0
          self.name = &quot;請輸入元素名稱&quot;
          self.center_x = 50
          self.center_y = 50
          self.ui_state = 0
      }
  }
  
  struct ContentView_Previews: PreviewProvider {
      static var previews: some View {
          ContentView()
      }
  }
  </code></pre>