Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Native: Show loading screen till the webview is loaded

I have at the moment a component SplashScreen which I'm rendering first till my state is set. I would like somehow to find a way how to still show this component while my webview is loaded. I added the onLoadEnd to my webview and looks like I get my message back when its finished loading, the problem is that if I load first the splashscreen and wait for the state to be changed onLoadEnd actually will never be changed because the webview is not yet rendered. Is there a good method how to do this?

like image 784
NinetyHH Avatar asked Sep 28 '16 22:09

NinetyHH


People also ask

How do I add a loading screen in react native?

Start by creating a new directory called src/screens/ and inside it, add the first file with the name HomeScreen. js and with the following code snippet: import React from 'react'; import {View, Text, StyleSheet, Pressable} from 'react-native'; const HomeScreen = ({navigation}) => { return ( <View style={styles.

How do I make Webview faster in react native?

For faster loading by pre loaded data, You should fetch data by WebView instance of fetch api. You can create a hidden WebView by setting width and height 0 and load your site on that. This will load your site on ViewView and keep cache, that will available for next time loading.


3 Answers

My solution was actually quite simple, the WebView component can have the param renderLoading which for me was not working, I figured out it was because also startInLoadingState needed to be defined.

So my WebView looks somehow like this:

<WebView
   ref={MY_REF}
   source={source}
   renderLoading={this.renderLoading}
   startInLoadingState
/>
like image 121
NinetyHH Avatar answered Oct 16 '22 23:10

NinetyHH


This would be my approach:

constructor(props){
    super(props);
    this.state = { webviewLoaded: false };
}

_onLoadEnd() {
    this.setState({ webviewLoaded: true });
}

render() {
    return(
        <View>
            {(this.state.webviewLoaded) ? null : <SplashScreen />}
            <WebView onLoadEnd={this._onLoadEnd.bind(this)} />
        </View>
    )
}

This way, the webview is rendered but it is placed behind the SplashScreen. So the webview starts loading while the SplashScreen is being displayed.

like image 44
JFAP Avatar answered Oct 17 '22 00:10

JFAP


I had a similar problem and I managed to solve it temporarily with this:

loadEnd () {
 this.setState({ webViewLoaded: true }):
}
render () {
const { webViewLoaded } = this.state;
return (<View> 
      {!webViewLoaded && <LoadingComponent /> } -- or spinner, whatever

         <WebView 
            style={(webViewLoaded) ? styles.webView : styles.loading}
            onLoadEnd={() => this.loadEnd.bind(this)} /> 
      </View);

}

const styles = StyleSheet.create({
  webView: { -- your styles ---},
  loading: {
    width: 0,
    heigt: 0
  }
});

not sure if exactly this helps you but you can try similar approach. I will probably change this to something more convenient. Not sure if there are possibilities to animate these changes because Im still pretty newbie in React Native.

edit: added hiding the spinner/loading element

like image 2
uRTLy Avatar answered Oct 17 '22 00:10

uRTLy