Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I Refresh Web View in React Native?

Tags:

react-native

I have a webview as tab A and a todolist flatlist on tab B. If the user adds an entry to the flatlist on tab B, i want the tab A webview to refresh.

I couldn't find any .refresh() or reload() methods on the webview control https://facebook.github.io/react-native/docs/webview.html

Any ideas how to accomplish this?

like image 332
tommy chheng Avatar asked Mar 27 '18 19:03

tommy chheng


People also ask

What are React Native webviews?

In React Native WebViews enable access to any web portal in the mobile app itself. In other words, a web view allows us to open the web URLs inside the app interface. While React Native provides us with a built-it web view component, but we are going to use react-native-webview plugin in this tutorial, since it is more powerful.

How to add pull to refresh functionality in React Native?

Pull to Refresh functionality is implemented using RefreshControl component in React Native. RefreshControl is used inside a ScrollView or ListView to add pull to refresh functionality. When the ScrollView is at scrollY: 0, swiping down triggers an onRefresh event. In our example, we are using a FlatList component to display the list.

How to create a home screen in React Native?

So visit my React Navigation Installation tutorial and follow step 1, 2 and 3. 2. Now open your react native project’s main App.js file and import useState, useEffect, StyleSheet, Text, View, Button, NavigationContainer, useIsFocused and createStackNavigator component. 3. Creating our first home screen functional component named as HomeScreen ().

How to update the react navigation screen when user comes back?

To achieve this functionality we have to use useIsFocused () method of React Navigation. We would use the useIsFocused () in any functional or class component. When user came back to previous screen it will check the useIsFocused () value and according to that it will re-render the component itself. This would update the screen.


Video Answer


2 Answers

You can set a key to the webview

key={this.state.key}

and then you can reload it by updating the state

this.setState({ key: this.state.key + 1 }); 
like image 109
prospegto Avatar answered Nov 03 '22 21:11

prospegto


Well I reload WebView by doing following:

render() {
  let WebViewRef;
  return (
    <View style={Style1.container}>
      <WebView
        ref={WEBVIEW_REF => (WebViewRef = WEBVIEW_REF)}
        source={{ uri: this.state.site }}
        renderLoading={this.ActivityIndicatorLoadingView}
        startInLoadingState={true}
      />
      <Button title="Reload Me!" onpress={() => { WebViewRef && WebViewRef.reload(); }} />
    </View>
  )
}

In this code I Declare Reference Variable WebViewRef first then assign this to WebView as ref={WEBVIEW_REF => (WebViewRef = WEBVIEW_REF)} and then call this reference for reload() as ()=>{ WebViewRef && WebViewRef.reload();}

like image 25
Imran Noor Avatar answered Nov 03 '22 23:11

Imran Noor