【问题标题】:Show loading indicator between navigation of pages in a Webview在 Web 视图中的页面导航之间显示加载指示器
【发布时间】:2020-06-28 06:32:06
【问题描述】:

我在 Web 视图中有一个 url,例如 https://www.nytimes.com/ 当前代码在初始页面加载时工作,但如果我键入点击链接中的任何内容,页面需要一段时间才能加载,并且网站中没有加载指示器。当我们点击任何链接或页面加载时,有什么方法可以在 React Native 中放置一个页面加载指示器,特别是如果它是像下一个 js 那样在服务器端呈现的?

这是我的 ReactNative 代码。

import * as React from 'react';
import {
  View,
  Text,
  Image,
  SafeAreaView,
  ScrollView,
  TextInput,
  TouchableOpacity,
} from 'react-native';
import styles from './styles';
import { WebView } from 'react-native-webview';
// import WelcomeSwiper from '../../components/WelcomeScreen/WelcomeSwiper';

import LoadingIcon from '../../components/Loading/index.js';

const WebView = ({ navigation }) => {


  return (
    <SafeAreaView style={styles.container}>

      <WebView
        javaScriptEnabled={true}
        domStorageEnabled={true}
        renderLoading={LoadingIcon}
        source={{ uri: 'https://www.nytimes.com/ ' }}
        startInLoadingState={true}

      />
    </SafeAreaView>
  );
};

export default WebView;

这是我的加载组件

import React from 'react';
import { StyleSheet, Platform, ActivityIndicator } from 'react-native';


const LoadingIcon = () => {
    return (

        <ActivityIndicator
            color='#009688'
            size='large'
            style={styles.ActivityIndicatorStyle}
        />
    );
}

export default LoadingIcon;


const styles = StyleSheet.create(
    {

        WebViewStyle:
        {
            justifyContent: 'center',
            alignItems: 'center',
            flex: 1,
            marginTop: (Platform.OS) === 'ios' ? 20 : 0
        },

        ActivityIndicatorStyle: {
            position: 'absolute',
            left: 0,
            right: 0,
            top: 0,
            bottom: 0,
            alignItems: 'center',
            justifyContent: 'center'

        }
    });

【问题讨论】:

  • 是您的代码无法正常工作还是需要其他解决方案?
  • 此代码在页面第一次加载时有效,但页面加载后页面加载中的链接不起作用

标签: reactjs react-native webview


【解决方案1】:

我们可以使用这两种方法得到结果:

  1. 您可以使用onLoadProgress 方法检查WebView 是否正在加载某些内容。此方法为您提供一个介于 0 和 1 之间的数字。如果页面已完全加载,它将返回数字 1,更新您的状态并根据它显示 ActivityIndi​​cator:

  2. 您可以使用onLoadStartonLoadEnd 来更新您的状态并根据它显示ActivityIndi​​cator!

更多信息请查看:https://github.com/react-native-community/react-native-webview/blob/master/docs/Reference.md#onloadprogress

你也可以使用你的ActivityIndicator 由WebView包装,*不要忘记这个方法适用于ios for android把它放在WebView之外

这是一个适合您的工作代码示例:

import React, {useState} from 'react';
import {View, Text, SafeAreaView, ActivityIndicator} from 'react-native';
import {WebView} from 'react-native-webview';

function WebViewTest() {
  const [isLoading, setLoading] = useState(false);
  return (
    <SafeAreaView style={{flex: 1}}>
      <WebView
        source={{uri: 'https://www.nytimes.com/'}}
        onLoadStart={(syntheticEvent) => {
          setLoading(true);
        }}
        onLoadEnd={(syntheticEvent) => {
          setLoading(false);
        }} />
        {isLoading && (
          <View style={{flex: 10, backgroundColor: 'white'}}>
            <ActivityIndicator
              color="#009688"
              size="large"
            //   style={{position: 'absolute', left: 200, top: 300}}
            />
          </View>
        )}
    </SafeAreaView>
  );
}

export default WebViewTest;

希望对你有帮助

【讨论】:

  • 我猜 web 视图不能有子我收到这个错误 RNCWebViewManager cannot be cast to ViewGroupManager
  • 正如我提到的,在 ios 中您可以将组件作为子组件传递,但在 Android 中您应该将组件放在 WebView 之外~~~,您也可以在外部实现它以获得更简洁的代码!
  • 你能解释一下 IOS 和 Android 的 hack
  • 你需要这个吗?
  • 是的,但是看到页面是第一次加载,但是在页面内的链接期间它们需要一些时间来加载
【解决方案2】:

我使用onLoadProgress 来解决这个问题。

renderLoading 函数只是在 webview 组件的初始加载状态下被调用。使用 renderLoading 无助于在任何点击页面链接或在 web 视图页面之间导航时显示活动指示器。

检查onLoadStartonLoadEnd 在android 中很有用,但在iOS 中onLoadEnd 不会在返回导航手势中调用,这会导致活动指示器无休止地旋转。

onLoadProgress 在 webview 处于加载状态时返回一个 0-1 之间的数字。您检查其进度状态并更新您的活动指示器状态。

更多关于onLoadProgress的信息:onLoadProgress

这是一个适合您的工作代码示例:

import React, {useState} from 'react';
import {View, Text, SafeAreaView, ActivityIndicator} from 'react-native';
import {WebView} from 'react-native-webview';

function WebViewTest() {
  const [isLoading, setLoading] = useState(false);
  return (
    <SafeAreaView style={{flex: 1}}>
      <WebView
        source={{uri: 'https://www.nytimes.com/'}}
        onLoadProgress={({nativeEvent}) => {
                    if (nativeEvent.progress != 1 && isLoading == false ) {
                        setLoading(true)
                    } else if (nativeEvent.progress == 1 ) {
                        setLoading(false)
                    }
                }} 
        />
        {isLoading && (
          <View style={{flex: 10, backgroundColor: 'white'}}>
            <ActivityIndicator
              color="#009688"
              size="large"
            //   style={{position: 'absolute', left: 200, top: 300}}
            />
          </View>
        )}
    </SafeAreaView>
  );
}

export default WebViewTest;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多