Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在消息不';我什么也不做_Javascript_Html_React Native_Webview - Fatal编程技术网

Javascript 在消息不';我什么也不做

Javascript 在消息不';我什么也不做,javascript,html,react-native,webview,Javascript,Html,React Native,Webview,我正在尝试使用onmessagelistener。网站正在执行postMessage(window.postMessage(“来自web的postMessage”);),但react native的webview onMessage listener没有执行任何操作!我不知道我做错了什么 这是HTML <script type="text/javascript"> window.postMessage("Post message from web"); </script&g

我正在尝试使用onmessagelistener。网站正在执行postMessage(
window.postMessage(“来自web的postMessage”);
),但react native的webview onMessage listener没有执行任何操作!我不知道我做错了什么

这是HTML

<script type="text/javascript">
  window.postMessage("Post message from web");
</script>
这里还有一份世博小吃。。。我不知道我做错了。。。

根据,您需要等待,直到React Native
postMessage
替换了Native
窗口。postMessage
(不要问我他们为什么要替换本机函数而不是创建新函数)

一种解决方案是执行以下操作:

function waitForBridge() {

   //the react native postMessage has only 1 parameter
   //while the default one has 2, so check the signature
   //of the function

   if (window.postMessage.length !== 1){
     setTimeout(waitForBridge, 200);
   }
   else {
     window.postMessage('abc');
   }
}

window.onload = waitForBridge;

如果您像我一样在
v5.0.0
之后使用
react native webview
,请使用
window.react native webview.postMessage(数据)

window.postMessage(数据,*)已更改为

window.ReactNativeWebView.postMessage(数据)


对于仍然对此感到困惑的人来说……这是因为React Native和Webview之间的通信已被完全重写。是的,window.postMessage(数据,*)已更改为window.React NativeWebView.postMessage(数据),但要具体回答此问题,解决方案(即,您需要从web视图中支持window.postMessage的内容)是为每个用户传递以下道具:

const injectedJavascript=`(函数(){
window.postMessage=函数(数据){
window.ReactNativeWebView.postMessage(数据);
};
})()`
this.webView=webView}
onMessage={this.onMessage}
source={{uri:'https://app.sodge.co/login/response.html'}}

/>
这解决了我的问题

让postMessage=window.parent.postMessage;
if(window.ReactNativeWebView){
postMessage=window.ReactNativeWebView.postMessage;
}
postMessage('hey');

难以置信!我花了几个小时试图找出为什么我没有收到任何回复。2019年5月。对本机expo sdk 32.0.0做出反应,但仍然是这个愚蠢的问题。关于“为什么”到“…替换本机函数”的问题仍然是完全正确的!这是真正的答案。这应该是正确的答案。是的,问题是关于接收我但原因是
postMessage
不起作用。
onMessage( event ) {
  Alert.alert(
    'On Message',
    event.nativeEvent.data,
    [
      {text: 'OK'},
    ],
    { cancelable: true }
  )
}
function waitForBridge() {

   //the react native postMessage has only 1 parameter
   //while the default one has 2, so check the signature
   //of the function

   if (window.postMessage.length !== 1){
     setTimeout(waitForBridge, 200);
   }
   else {
     window.postMessage('abc');
   }
}

window.onload = waitForBridge;