【问题标题】:Webview freezed after using the stopLoading of react-native-webview使用 react-native-webview 的 stopLoading 后 Webview 冻结
【发布时间】:2021-02-17 04:01:03
【问题描述】:

我想在 React 本机 Webview 中打开一个网页,并在默认浏览器中打开 href(链接)。我尝试使用 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 }}
      />    
    );
  }

【问题讨论】:

  • 我发现随机行为很奇怪。但是我查看了我的代码,发现我在外部打开 URL 之后添加了“this.webview.goBack()”,然后我返回 true 而不是 false。作为一个好的解决方案,这肯定有点难。不幸的是,我已经有一段时间没有从事那个项目了,所以我担心,我在这里没用了'^^。

标签: javascript react-native webview react-navigation react-native-webview


【解决方案1】:

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

webViewRef.current.stopLoading();

其中 webViewRef 是 webView 的引用

ref = { webViewRef }

【讨论】:

    猜你喜欢
    • 2019-11-24
    • 1970-01-01
    • 2017-09-01
    • 2019-01-31
    • 2015-09-23
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 2023-02-25
    相关资源
    最近更新 更多