[英]navigate to a react native page from webview
I have a react native app that uses webView to render an html code , i used postmessage for communicating between them. 我有一个使用webView呈现html代码的react native应用程序,我使用postmessage在它们之间进行通信。 webview will send message to react native to open another react native page ( for example send message 'open login page' ) so i will recieve it in onMessage (event) , then when i want to use
this.props.navigateTo
it says 'can not read property navigateTo of undefined' . webview将发送消息以响应本机打开另一个响应本机页面(例如,发送消息“打开登录页面”),因此我将在onMessage(事件)中接收它,然后当我想使用
this.props.navigateTo
时说“可以无法读取未定义的属性navigationTo”。 i used console.log(this)
and understood 'this' is DedicatedWorkerGlobalScope , that i can't use navigatTo()
. 我使用
console.log(this)
并理解'this'是DedicatedWorkerGlobalScope ,我不能使用navigatTo()
。 how can i use navigatTo in this case (while im in DedicatedWorkerGlobalScope) or how can i close or stop rendering webview to do this? 在这种情况下(当我在DedicatedWorkerGlobalScope中时,我该如何使用navigatTo?)如何关闭或停止渲染Webview来做到这一点?
onMessage( event ) {
let post = JSON.parse(event.nativeEvent.data);
console.log(event)
switch(post.message){
case 'open login page':
console.log(this); // 'DedicatedWorkerGlobalScope'
this.props.navigateTo('login'); //'can not read property navigation of undefined'
break;
}
}
this is the webview : 这是webview:
<WebView
// onNavigationStateChange={async (e) => {
// console.log(e);
// if (async() => await getKey('isLogged') === false)
// this.props.navigateTo('login') //its ok here
// }}
source={{uri: isAndroid ?'file:///android_asset/html/index.html'
:'./html/index.html'}}
ref={ webView => this.webView = webView }
onMessage={this.onMessage}
/>
i use this.props.navigatTo()
for navigating and its ok but in this case state doesnt change to run this here , and this is my stacknavigator : 我使用
this.props.navigatTo()
进行导航,并且可以,但是在这种情况下,状态不会改变,可以在此处运行,这是我的stacknavigator:
login: {
screen: Login
},
app: {
screen: App // webview defined here
},
我自己解决了问题:))我应该使用bind,OnMessage = {this.onMessage.bind(this)}我只是忘记了...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.