I’m using react-native-webview
and attempting to post a message from JavaScript to RN.
I’m using the window.ReactNativeWebView.postMessage()
method but cannot get beyond some issue with ReactNativeWebView
not being recognised, with the following error:
Cannot read property 'postMessage' of undefined
I’m using the following:
This looks so straight forward so I don’t know where I’m going wrong.
Declare and use the reference value.
onLoadEnd() {
this.webview.postMessage("yourmessage");
}
<WebView
ref={webview => (this.webview = webview)}
onLoadEnd={() => this.onLoadEnd()}
...
/>
Your webview_uri should have a message-receiver just like React-native's.
webpage example
document.addEventListener("message", function(event) {
console.log("Received post message", event);
logMessage(event.data);
}, false);