title: SwiftUI - Updating @State when Global changes
body: <p>I'd like to update an UI element on an overview view when data on another view is changed.  </p>
  
  <p>I looked into @EnvironmentalObject and @Binding. However, an update to either object does not appear to force a view reload. Only changes to @State force renders.<br>
  Also, in the case described below, the ChangeView is not a child of OverviewView. Therefore @Binding is not an option anyway.</p>
  
  <p>Data.swift</p>
  
  <pre class="lang-swift prettyprint-override"><code>struct ExampleData : Hashable {
   var id: UUID
   var name: String
  }
  
  var globalVar: ExampleData = ExampleData(id: UUID(), name:"")
  </code></pre>
  
  <p>OverviewView.swift</p>
  
  <pre class="lang-swift prettyprint-override"><code>struct OverviewView: View {
      @State private var data: ExampleData = globalVar
      var body: some View {
         Text(data.name)
      }
  }
  </code></pre>
  
  <p>ChangeView.swift</p>
  
  <pre class="lang-swift prettyprint-override"><code>struct ChangeView: View {
      @State private var data: ExampleData = globalVar
      var body: some View {
         TextField("Name", text: $data.name, onEditingChanged: { _ in 
            globalVar = data }, onCommit: { globalVar = data })
      }
  }
  </code></pre>
  
  <p>Changes within the ChangeView TextField will update the globalVar. However, this will not update the Text on the OverviewView when switching back to the view.</p>
  
  <p>I am aware that using global variables is "ugly" coding.  How do I handle data that will be used in a multitude of unrelated views?  </p>
  
  <p>Please advise on how to better handle such a situation.</p>