javascript - React-Native 预加载 uri 而 webview 不可见

标签 javascript webview react-native

我有一个 react 如下的 WebView 。

      <WebView
        source={{ uri: this.state.url }}
        style={ this.state.style }
        javaScriptEnabled = { true }
        startInLoadingState = {true}
        onLoad = {this.showPage()}
        onLoadEnd = {this.showPage()}
      />

当我设置 this.state.url 时,我希望 webview 立即加载 url,然后在加载 onLoad 或 onLoadEnd 后调用 showPage 函数来显示 webview 内容。我似乎无法让它工作 - web View 仅在 dom 中可见时加载 url。有办法做到这一点吗?

最佳答案

最简单的解决方案是使用 WebView 的 renderLoading 属性。如果您需要自定义加载程序逻辑,您可以使用如下内容:

class WebScreen extends Component {
  ...
  render() {
    const { isLoading } = this.state;
    return (
      <View style={styles.container}>
        <ReactWebView
          style={styles.webview}
          source={{ uri: url }}
          onLoadStart={::this.onLoadStart}
          onLoadEnd={::this.onLoadEnd}
        />
        {isLoading && (
          <WebViewLoader style={styles.loader} />
        )}
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1
  },

  loader: {
    position: 'absolute',
    top: 0,
    right: 0,
    bottom: 0,
    left: 0,
    zIndex: 4,
    backgroundColor: 'white'
  }
});

关于javascript - React-Native 预加载 uri 而 webview 不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39333215/

相关文章:

javascript - 在 React Native 中从数组中随机选择图像

javascript - 将数字减少到最接近的 3 的倍数

android - 在不关闭 Activity 的情况下切换 Activity

c# - Android C# WebView OnPageStart toast

ios - self Controller nil webview

android - 当应用程序在后台时将用户位置更新到服务器

ios - RCTBatchedBridge 已弃用,将在未来的 React Native 版本中删除

javascript - 当 "change"被触发时检索 ckeditor 文本更改的某种方法

javascript - Push is not a function JavaScript 错误

javascript - 如何从 View 重新加载部分 View ?