title: How to inject JavaScript in WebView React Native
body: <p>I have different types of scripts that need to be executed through WebView in the app. I followed many other related posts, but those didn't help me much. The first one is executing fine.</p>
  <pre><code>setTimeout(function() {
   window.ReactNativeWebView.postMessage(JSON.stringify(${window.CartVue.carts}))
  }, 2000);true;
  </code></pre>
  <p>For the second one, can someone help me execute the below JavaScript code in WebView?</p>
  <pre><code>async function extractItems(){
     const responsePromise = await fetch('https://www2.hm.com/tr_tr/v1/carts',
    {
      credentials: 'include'});
      const response = await responsePromise.json();
      return response;
    }
    await extractItems();
  </code></pre>
  <p><strong>WebView</strong></p>
  <pre><code>&lt;WebView
    ref={webViewRef}
    source={{uri: webUrl}}
    onNavigationStateChange={webViewState =&gt; {
      // setWebUrl(webViewState?.url);
    }}
    javaScriptEnabled={true}
    domStorageEnabled={true}
    startInLoadingState={true}
    injectedJavaScript={jsCode}
    onLoadEnd={() =&gt; onLoadWebUrl()}
    renderLoading={() =&gt; &lt;Loading visible /&gt;}
    onError={() =&gt; webViewRef.current.reload()}
    onMessage={event =&gt; {
      console.log(&quot;onMessage event ==&gt; &quot;,);
      if (cartUrl !== '') {
        onMessage(event);
      }
    }}
  /&gt;
  </code></pre>