使用 react-native-webview 的 stopLoading 后 Webview 冻结 [英] Webview freezed after using the stopLoading of react-native-webview

查看:46
本文介绍了使用 react-native-webview 的 stopLoading 后 Webview 冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to open a webpage in React native Webview and the hrefs (links) to be opened in the default browser. I tried using the stopLoading method of Webview but it freezes the view. I successfully achieved this using the answer on this thread.

react-native-webview的stopLoading方法导致网站卡死

但在某些情况下,随机点击 href 链接后,除了在默认浏览器中打开网页外,它还会在 Webview 中打开网页.我只想在浏览器中打开它.请检查我在这里做错了什么.或者有没有更好的方法来实现这一目标?

But in some cases, randomly, Upon click of href link, it opens the webpage in the Webview as well beside opening it in the default browser. I want it to be opened in the brwoser only. Please check what's I am doing wrong here. Or is there any better approach to achieve this?

这是我使用的代码:

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

webViewRef.current.stopLoading();

其中 webViewRef 是 webView 的引用

where webViewRef is the ref of the webView

ref = { webViewRef }

这篇关于使用 react-native-webview 的 stopLoading 后 Webview 冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆