微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Webview在使用react-native-webview的stopLoading后冻结

如何解决Webview在使用react-native-webview的stopLoading后冻结

我想在React本机Webview中打开一个网页,并在认浏览器中打开hrefs(链接)。我尝试使用Webview的stopLoading方法,但是它冻结了视图。我使用此线程上的答案成功实现了这一目标。

The method stopLoading of react-native-webview causes the website to freeze

但是在某些情况下,随机地,单击href链接后,除了在认浏览器中打开网页外,它还会在Webview中打开网页。我希望仅在浏览器中将其打开。请在这里检查我在做什么错。还是有更好的方法来实现这一目标?

这是我正在使用的代码

LoadingIndicatorView() {
    return (
      <ActivityIndicator
        color="#009b88"
        size="large"
        style={{ flex: 1,justifyContent :'center',}}
      />
    );
  }

  handleWebViewRequest = request => {
    const {url} = request;
    Linking.openURL(url);
    return false;
  }

  render() {

    let uri = 'https://www.google.com/';
    let sku = this.props.route.params.sku;
    let location = this.props.route.params.location;

    return (
      <WebView
      ref={ref => (this.webview = ref)}  
      source={{ uri: uri,method: 'POST',body: 'device=tablet&search='+sku+'&ref='+location }}
      renderLoading={this.LoadingIndicatorView}
      startInLoadingState={true}
      onShouldStartLoadWithRequest={this.handleWebViewRequest.bind(this)}
      style={{ flex: 1 }}
      />    
    );
  }

解决方法

我在为 onShouldStartLoadWithRequest 调用的方法中添加了 stopLoading

webViewRef.current.stopLoading();

其中 webViewRef 是 webView 的引用

ref = { webViewRef }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。