How to display "load" message while Webview is loading?

Asked

Viewed 44 times

1

I need to display a message on screen while the Webview component has not fully loaded, but I don’t know how to detect if it has already loaded the content. Here is my code:

return (    
  <View style={styles.container}>
    <WebView 
      style={styles.webview}
      setSupportMultipleWindows={false}
      ref={this.WEBVIEW_REF}
      javaScriptEnabled={true}
      allowsFullscreenVideo={true}
      onNavigationStateChange={this.onNavigationStateChange}
      source={{ uri: 'https://pei.educacao.mg.gov.br/login' }}  />
  </View>
);

1 answer

1

I think a good solution here would be to use the compontente Activityindicator

It is a native component that can be customized, will be displayed in place of the component while it is loader, and can display it based on the state of its component, something like:

const App = () => {
  const [visible, setVisible] = useState(false);
  return (
      <View style={styles.container}>
        <WebView
          style={styles.webview}
          setSupportMultipleWindows={false}
          ref={this.WEBVIEW_REF}
          javaScriptEnabled={true}
          allowsFullscreenVideo={true}
          onNavigationStateChange={this.onNavigationStateChange}
          source={{ uri: 'https://pei.educacao.mg.gov.br/login' }}
          onLoadStart={() => setVisible(true)}
          onLoad={() => setVisible(false)}
        />
        {visible ? <ActivityIndicatorElement size="large" /> : null}
      </View>
  );
};

Here was used the visible and setVisiblie to control the visibility of WebView and change that with onLoadStart and onLoad

I have a component like this, it was based on this code: React-Native-show-Progress-bar-while-loading-webview

  • The answer is correct, just bounce here for attention when using Arrow functions in render. As mentioned in documentation: "When using an Arrow Function in render, a new function is created each time the component is rendered, which can break optimizations based on identity comparison on strict." I prefer to avoid and make use of a Eslint rule to alert myself to this: React/jsx-no-bind

Browser other questions tagged

You are not signed in. Login or sign up in order to post.