简体   繁体   English

如何在 react-native webView 和 React Web 应用程序之间进行通信?

[英]how to Communicate between react-native webView and a React web-app?

i'm trying to create a communication between a webView in react-native and a web-app created with React-360 (and of course React).我正在尝试在 react-native 中的 webView 和使用 React-360(当然还有 React)创建的网络应用程序之间创建通信。 I'm using the react-native-webview and I follow this guide at this link to create a communication: https://github.com/react-native-community/react-native-webview/blob/master/docs/Guide.md Using the example in the page, with a simple html page (like in the example in the link) with the function window.ReactNativeWebView.postMessage(data) in the script section it works good:我正在使用 react-native-webview 并在此链接上按照本指南创建通信: https://github.com/react-native-community/react-native-webview/blob/master/docs/Guide .md使用页面中的示例,使用简单的 html 页面(如链接中的示例)和 function window.ReactNativeWebView.postMessage(data)脚本部分 itdata 工作:

 <WebView
  source={{uri: this.props.link}}

    ref={( webView ) => this.webView = webView}

    onMessage={event => {
      alert(event.nativeEvent.data);
    }}
  />

My purpose, however, is to call in my web application inside the webView window.ReactNativeWebView.postMessage(data) when i click a button, for example with a function like this: My purpose, however, is to call in my web application inside the webView window.ReactNativeWebView.postMessage(data) when i click a button, for example with a function like this:

homeButtonClicked() {
    console.log("sei uscito, torna all'App!")
    window.ReactNativeWebView.postMessage(data);
}

Unfortunately, when i click the button i got the following error: Cannot read property 'postMessage' of undefined .不幸的是,当我单击按钮时,出现以下错误: Cannot read property 'postMessage' of undefined How can i resolve this problem?我该如何解决这个问题? Thanks to all.谢谢大家。

Can you try with timeout function like in the doc你能像文档中那样尝试超时 function

setTimeout(function () {
   window.ReactNativeWebView.postMessage("Hello!")
}, 2000)

I found myself the solution of the problem in an old issue inside React-360 repository.我在 React-360 存储库中的一个旧问题中找到了问题的解决方案。 React-360 code cannot access to window, so it is necessary to put the code inside a native module and from there it is possible to send a message to the react-native app. React-360 代码无法访问 window,因此有必要将代码放在本机模块中,然后可以从那里向 react-native 应用程序发送消息。 Link to the issue: https://github.com/facebook/react-360/issues/241问题链接: https://github.com/facebook/react-360/issues/241

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM