Search code examples
react-nativereact-native-webview

ReactNativeWebView is Not Recognised


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:

  • "react": "16.8.3",
  • "react-native": "0.59.8",
  • "react-native-webview": "^5.12.0",

This looks so straight forward so I don’t know where I’m going wrong.


Solution

  • 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);