'React native webview using script in html of source parameter works on iOS but not on android

I have this react native webview setup shown below. It works well with iOS but shows a blank page on Android.

I am using RN 0.63.3 and RNWebview 11.15.0

How can I resolve this issue?

<WebView
        onShouldStartLoadWithRequest={onRouteChange}
        javaScriptEnabled={true}
        injectedJavaScript={`(${ReportJS})()`}
        injectedJavaScriptForMainFrameOnly={true}
        onMessage={onMessage}
        onLoadStart={onLoadStart}
        style={styles.webview}
        ref={webViewRef}
        originWhitelist={['*']}
        source={{
          html: `
            <body>
              <script>${startupJS}</script>
            </body>
        `,
        }}
        
      />

This is the function inside ReportJS:

[window, document].forEach((el) =>
    el.addEventListener('load', (event) => {
      let data = 'success';
      if (location.href.startsWith('<redacted url>')) {
        const inputElement = document.querySelector(
          "form input[name='response']",
        );
        data = inputElement?.value;
      }
      if (data === undefined) {
        data = 'Hello!';
      }
      window.ReactNativeWebView.postMessage(data);
    }),
  );


Solution 1:[1]

Running the file set as injectedJavascript variable within the page instead of using an external file resolved my issue

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Viswen Kumar