alinz / react-native-webview-bridge

React Native Webview with Javascript Bridge
MIT License
1.37k stars 493 forks source link

WebViewBridge not available on initial document load #220

Open peterept opened 7 years ago

peterept commented 7 years ago

The sample did not work for me. The WebViewBridge variable was undefined when the document is loaded. Initially I added a small timeout, but am now using an event listener as shown below.


<script>
 (function () {
    function onWebViewBridgeReady() {
      document.removeEventListener('WebViewBridge', onWebViewBridgeReady, false);
      WebViewBridge.onMessage = function (message) {
          // assume message has a color 
          document.body.style.backgroundColor = message;
        }
     }
      WebViewBridge.send("webviewbridge-ready");
    }
   document.addEventListener('WebViewBridge', onWebViewBridgeReady, false)
  }())
</script>
HofmannZ commented 7 years ago

See #206

peterept commented 7 years ago

Thanks @HofmannZ.

Are there any advantages of this project? Like the JS injection?

HofmannZ commented 7 years ago

Not much since all of the devs moved the RN core implementation. There are still a couple of little bugs, though a load less than there are in this package. JS injection is also available in the core implementation, just use the injectedJavaScript prop, you can read up on it here.