繁体   English   中英

从Webview导航到React本机页面

[英]navigate to a react native page from webview

我有一个使用webView呈现html代码的react native应用程序,我使用postmessage在它们之间进行通信。 webview将发送消息以响应本机打开另一个响应本机页面(例如,发送消息“打开登录页面”),因此我将在onMessage(事件)中接收它,然后当我想使用this.props.navigateTo时说“可以无法读取未定义的属性navigationTo”。 我使用console.log(this)并理解'this'是DedicatedWorkerGlobalScope ,我不能使用navigatTo() 在这种情况下(当我在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;
   }
}

这是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}
        />

我使用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.

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