React native React Native WebView onMessage不起作用

React native React Native WebView onMessage不起作用,react-native,React Native,我的同伴 import React, {Component} from 'react'; import {WebView} from 'react-native'; class Html extends Component { constructor(props){ super(props); this.webView = null; } onMessage( event ) { console.log( "On Messag

我的同伴

import React, {Component} from 'react';
import {WebView} from 'react-native';

class Html extends Component {

    constructor(props){
      super(props);
      this.webView = null;
    }

    onMessage( event ) {
        console.log( "On Message", event.nativeEvent.data );
    }

    sendPostMessage(msg) {
        console.log( "Sending post message",msg );
        this.webView.postMessage(JSON.stringify(msg));
    }

    runJSInBackground (code) {
      this.webView.injectJavaScript(code)
    }


  render() {  
    return (
      <WebView
        originWhitelist={['*']}
        ref={( webView ) => this.webView = webView}
        onMessage={this.onMessage}
        onError={(e)=>console.log("WebView error: "+e)}
        source={{
          baseUrl:"include/",
          html: `
          <html>
              <head>
              </head>
            <body>
            <script>
            document.addEventListener("message",function(event) {
                console.log("Received post message", event); //Work!!!
            }, false);

            setTimeout(()=>window.postMessage("WhatsAppMANNN!!!"),5000) //doesn`t work ((
            </script>
            </body>
          </html>
          ` }}
      />
    )
  }

}

export default Html;
import React,{Component}来自'React';
从“react native”导入{WebView};
类Html扩展组件{
建造师(道具){
超级(道具);
this.webView=null;
}
onMessage(事件){
console.log(“在消息上”,event.nativeEvent.data);
}
sendPostMessage(msg){
console.log(“发送post消息”,msg);
this.webView.postMessage(JSON.stringify(msg));
}
runJSInBackground(代码){
this.webView.injectJavaScript(代码)
}
render(){
返回(
this.webView=webView}
onMessage={this.onMessage}
onError={(e)=>console.log(“WebView错误:+e)}
来源={{
baseUrl:“包括/”,
html:`
document.addEventListener(“消息”、函数(事件){
console.log(“收到的帖子”,事件);//工作!!!
},假);
setTimeout(()=>window.postMessage(“WhatsAppMANNN!!!”),5000)//不起作用((
` }}
/>
)
}
}
导出默认Html;
当我发送WebView请求时,一切正常,但返回结果没有答案。也就是说,window.postMessage方法不起作用,并且错误也不会亮起。问题是什么,而不是我如何找不到这个问题的答案。

尝试使用:

window.postMessage("Lulz", "*") 

来源:

尝试webView.postMessage(“WhatsAppMANNN!!!”,5000)没有帮助,尝试并等待网桥函数waitForBridge(){if(window.postMessage.length!==1)setTimeout(waitForBridge,200);else window.postMessage('ABCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC