lucasferreira / react-native-webview-android

Simple React Native Android module to use Android's WebView inside your app
355 stars 158 forks source link

How do I receive a message sent by a webview in an html file when I load html? #110

Open xpbtmusic opened 6 years ago

xpbtmusic commented 6 years ago

How do I receive a message sent by a webview in an html file when I load html? The official webview is usingdocument.addEventListener('message', function (e) {}

lucasferreira commented 6 years ago

Hi @xpbtmusic

If you need just sent a data when your html file load, you could use the prop injectedJavaScript instead.

Something like:

return (
  <WebViewAndroid
    ref="webViewAndroidSample"
    javaScriptEnabled={true}
    injectedJavaScript={`window.myMessageToRecover = 'Hello from React Native';`}
    url={"https://yourwebsite.com"}
    style={{ flex: 1 }} />
);

And then, in your HTML you could recover the variable window.myMessageToRecover in on page ready or in your page load events.