Actually, in my case, this solution worked weel
import React, { useEffect, useRef } from 'react';
import { StyleSheet, View, BackHandler } from 'react-native';
import { colors, variables } from 'utils/theme';
import { WebView } from 'react-native-webview';
import { Button, Spinner, Text } from 'components';
import { fa } from 'utils/constants/locales';
const uri = YOUR_WEB_PAGE_URL
const Loading = () => {
return (
<View style={styles.loadingWrapper}>
<Spinner />
<Text style={styles.loading}>{fa.proEducation.loading}</Text>
</View>
);
};
const Error = ({ reload }) => {
return (
<View style={styles.loadingWrapper}>
<Button
style={styles.retry}
label={fa.proEducation.retry}
primary
onPress={reload}
/>
</View>
);
};
const ProEducation = () => {
const webview = useRef(null);
const canGoBackRef = useRef(false);
const onAndroidBackPress = () => {
if (canGoBackRef.current && webview.current) {
webview.current.goBack();
return true;
}
return false;
};
useEffect(() => {
BackHandler.addEventListener('hardwareBackPress', onAndroidBackPress);
return () => {
BackHandler.removeEventListener('hardwareBackPress', onAndroidBackPress);
};
}, []);
const onNavigationStateChange = ({ canGoBack }) => {
canGoBackRef.current = canGoBack;
};
const reload = () => webview.current.reload();
return (
<View style={styles.wrapper}>
<WebView
ref={webview}
source={{ uri }}
style={styles.webView}
onNavigationStateChange={onNavigationStateChange}
javaScriptEnabled
domStorageEnabled
renderLoading={() => <Loading />}
renderError={() => <Error reload={reload} />}
startInLoadingState
/>
</View>
);
};
const styles = StyleSheet.create({
loading: {
color: colors.lightBlack,
fontSize: 15,
marginTop: 8,
textAlign: 'center',
},
loadingWrapper: {
backgroundColor: colors.white,
bottom: 0,
flex: 1,
justifyContent: 'center',
left: 0,
marginBottom: 'auto',
marginLeft: 'auto',
marginRight: 'auto',
marginTop: 'auto',
position: 'absolute',
right: 0,
top: 0,
},
retry: {
alignSelf: 'center',
paddingHorizontal: variables.gutter,
paddingVertical: variables.gutter / 2,
},
webView: {
flex: 1,
},
wrapper: {
backgroundColor: colors.bg,
flex: 1,
},
});
export default ProEducation;