title: Where is the proper place for re-injecting Javascript (but not the html) in SwiftUI?
body: <p>I am trying to find the best place for re-injecting Javascript in the following two contexts:</p>
  <p><strong>On-demand injection (only when user presses a button):</strong></p>
  <p>Right now, I have the Javascript injection in my WKWebkit updateUIView() function (WKWebkit is wrapped as UIViewRepresentable) but it is being executed too often. (In addition to my state variable change, other circumstances trigger it, like tabbing to a from that view, for instance.) Alternatively, I have tried putting the actual injection code in a Button action.  But to do this, I needed access to the webview’s evaluateJavascript() func and the only way I could figure out how to get a handle on the webview was to do a one-time assignment to a variable in makeUIView(). This worked but I got a worrisome warning: “Modifying state during view update, this will cause undefined behavior”.
  I’m sure there is a better way than either of these two solutions.</p>
  <p><strong>Dynamic injections (done dynamically, to show effects in real-time, as a user is altering settings):</strong></p>
  <p>Similar to the above, I put the injection in updateUI(). It works but the injection happens more often than desired. Is there some other place for the injection?</p>
  <p>Note that both of these scenarios read a local html file. I would like to keep the injections separate from html loading, as it's not necessary in my case.</p>
  <p>Here is a simplified, working version of my code, which shows both scenarios.
  You will need to add the html file (below) to the project.</p>
  <p>Many thanks for your advice!</p>
  <pre><code>import SwiftUI
  import WebKit
  
  struct ContentView: View {
      @State private var webviewNum = 0
      @State private var tmpNum = 0
      
      var body: some View {
          VStack {
              Stepper (&quot;Change webview dynamically: - \(self.webviewNum)&quot;, value: self.$webviewNum, in: 0...100)
              
              Divider()
              
              Stepper (&quot;Update webview only when -Inject- pressed:  - \(self.tmpNum)&quot;, value: self.$tmpNum, in: 0...100)
              Button(action: {
                  self.webviewNum = self.tmpNum
                  
                  // If I put the Javascript injection here, I need a handle for my webview.
                  // How to properly obtain that?
              }) {
                  Text(&quot;Inject&quot;)
              }
              
              Divider()
              
              webView(webviewNum: self.$webviewNum)
          }
      }
  }
  
  struct webView: UIViewRepresentable {
      @Binding var webviewNum: Int
      
      func makeUIView(context: Context) -&gt; WKWebView {
          let view = WKWebView()
          let url = Bundle.main.url(forResource: &quot;source&quot;, withExtension: &quot;html&quot;)
          view.loadFileURL(url!, allowingReadAccessTo: url!.deletingLastPathComponent())
          
          // I could capture a reference to the view here (@Binding var myView = WKWebView)
          // but I get the warning: &quot;Modifying state during view update, this will cause undefined behavior&quot;
          return view
      }
      
      func updateUIView(_ uiView: WKWebView, context: Context) {
          
          print (&quot;updateUIView - inject&quot;)
          // Inject the Javascript
          DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
              let js = &quot;showTotal(\(self.webviewNum));&quot;
              uiView.evaluateJavaScript(js) { (_, error) in
                  if error != nil { print(&quot;Error: \(String(describing: error))&quot;) }
              }
          }
      }
  }
  
  struct ContentView_Previews: PreviewProvider {
      static var previews: some View {
          ContentView()
      }
  }
  
  
  </code></pre>
  <p>&quot;source.html&quot;:</p>
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;title&gt; test &lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h1 id=&quot;demo&quot;&gt;&lt;/h1&gt;
  
      &lt;script&gt;
          function showTotal(num) {
                  document.getElementById(&quot;demo&quot;).innerHTML = 'Via Javascript injection: ' + num ;
          };
      &lt;/script&gt;
      
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>