react-native webview加载指示器

Ale*_*ung 14 react-native-ios

我试图在webweb中显示加载指示符如下.正在显示加载指示器,但页面加载后会显示白色背景.如果我将startInLoadingState更改为false,则会显示Web内容,但不会显示加载指示符.它正在ios中"反应原生":"0.46.3"

renderLoadingView() {
    return (
        <ActivityIndicator
           animating = {this.state.visible}
           color = '#bc2b78'
           size = "large"
           style = {styles.activityIndicator}
           hidesWhenStopped={true} 
        />
    );
Run Code Online (Sandbox Code Playgroud)

}

<WebView
    source={source} 
    renderLoading={this.renderLoadingView} startInLoadingState={true} />
Run Code Online (Sandbox Code Playgroud)

Ada*_*amG 21

我喜欢这种方法,它显示了加载Webview上覆盖的活动指示器,因此您不必等到加载整个页面才能开始查看内容.

constructor(props) {
    super(props);
    this.state = { visible: true };
  }

  hideSpinner() {
    this.setState({ visible: false });
  }

  render() {
    return (
      <View style={{ flex: 1 }}>
        <WebView
          onLoad={() => this.hideSpinner()}
          style={{ flex: 1 }}
          source={{ uri: this.props.navigation.state.params.url }}
        />
        {this.state.visible && (
          <ActivityIndicator
            style={{ position: "absolute", top: height / 2, left: width / 2 }}
            size="large"
          />
        )}
      </View>
    );
  }
Run Code Online (Sandbox Code Playgroud)

  • 迄今为止最好的解决方案!要居中加载,只需添加`style={{position:'absolute',left:0,right:0,bottom:0,top:0,}}` (7认同)

小智 8

一种不错的方法是将属性startInLoadingState设置为true,然后将renderLoading设置为返回所需的View。请参见下面的示例。

  displaySpinner() {
    return (
      <View>
        {/* Your spinner code goes here. 
          This one commes from react-native-material-kit library */}
        <SingleColorSpinner />
      </View>
    );
  }

  render() {
    return (
      <WebView
        startInLoadingState={true}
        source={{ uri: this.state.myUri }}
        renderLoading={() => {
          return this.displaySpinner();
        }}
      />
    );
  }
Run Code Online (Sandbox Code Playgroud)

  • 除非我遗漏了一些东西,否则这只在初始页面加载时显示加载指示器,正确吗?(如果用户单击网络视图中的链接,它不会重新显示它。) (4认同)

Tia*_*nes 5

我已经解决了这个问题,经过一些研究,我找到了一个很好的解决方案。

它需要“ react-native-loading-spinner-overlay”

npm install --save react-native-loading-spinner-overlay
Run Code Online (Sandbox Code Playgroud)

index.android.js

import Spinner from 'react-native-loading-spinner-overlay';

const main = 'http://www.myURI.pt';

class MyApp extends Component {
    constructor(props) {
        super(props);
        this.state = { uri: main, visible: true };
    }

    showSpinner() {
        console.log('Show Spinner');
        this.setState({ visible: true });
    }

    hideSpinner() {
        console.log('Hide Spinner');
        this.setState({ visible: false });
    }

    render() {
        return (
            <View>
                <Spinner
                    visible={this.state.visible}
                    textContent={'Loading...'}
                    textStyle={{ color: '#FFF' }}
                />
                <WebView
                    scalesPageToFit
                    source={{ uri: this.state.uri }}
                    onLoadStart={() => (this.showSpinner())}
                    onLoad={() => (this.hideSpinner())}
                />
            </View>
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

我想我没有错过任何一行。


Sha*_*awn 5

React-Native WebView 现已弃用。
您可以导入react-native-webview并执行以下操作:

    <WebView
    source={{ uri: 'https://reactnative.dev' }}
    startInLoadingState={true}
    renderLoading={() => <Loading />}
    />
Run Code Online (Sandbox Code Playgroud)


Rau*_*qss 4

将您的函数更改renderLoadingView为以下内容,加载指示器应该按预期工作:

renderLoadingView() {
  return (
    <ActivityIndicator
      color='#bc2b78'
      size='large'
      styles={styles.activityIndicator}
    />
  );
}
Run Code Online (Sandbox Code Playgroud)

因此,本质上,只需animatinghidesWhenStopped您的ActivityIndicator. 希望这可以帮助。