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?
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.
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.
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 ().
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.
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 });
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();}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With