Webview not responding to goBack() or injectJavascript()
See original GitHub issueHello, I’ve noticed that the expo webview is not responding to goBack() or injectJavascript even if the reference is not null. Considering the below code sample, console.log(“backb”); prints correctly a reference, but javascript is not injected and webview won’t go back in any way
import { StatusBar } from 'expo-status-bar';
import React, { useState, useRef, useEffect } from 'react';
import { ActivityIndicator, Linking, SafeAreaView,StyleSheet, BackHandler, Button} from 'react-native';
import { WebView } from 'react-native-webview';
export default function App() {
const webViewRef = useRef()
const [isLoading, setLoading] = useState(false);
const handleBackButtonPress = () => {
console.log("backb");
const run = `document.getElementsByClassName('gLFyf')[0].value = 'World';`;
try {
webViewRef.current.injectJavaScript(run);
console.log(webViewRef.current)
webViewRef.current?.goBack()
} catch (err) {
console.log("[handleBackButtonPress] Error : ", err.message)
}
return false;
}
useEffect(() => {
BackHandler.addEventListener("hardwareBackPress", handleBackButtonPress)
return () => {
BackHandler.removeEventListener("hardwareBackPress", handleBackButtonPress)
};
}, []);
return (
<SafeAreaView style={styles.safeArea}>
<WebView
originWhiteList={['*']}
source={{ uri: 'https://google.com' }}
style={styles.container}
ref={webViewRef}
onLoadStart={(syntheticEvent) => {
setLoading(true);
}}
onShouldStartLoadWithRequest={(event)=>{
if (event.navigationType === 'click') {
if (!event.url.match(/(google\.com\/*)/) ) {
Linking.openURL(event.url)
return false
}
return true
}
else{
return true;
}
}}
onLoadEnd={(syntheticEvent) => {
setLoading(false);
}}
/><Button
onPress={handleBackButtonPress}
title="GoBack"
color="#841584"
/>
{isLoading && (
<ActivityIndicator
color="#234356"
size="large"
style={styles.loading}
/>
)}
</SafeAreaView>
);
}
const styles = StyleSheet.create({
safeArea: {
flex: 1,
backgroundColor: '#234356'
},
loading: {
position: 'absolute',
left: 0,
right: 0,
top: 0,
bottom: 0,
alignItems: 'center',
justifyContent: 'center'
},
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
I’m testing it with expo app in Android
- react-native version: “0.68.2”,
- react-native-webview version: “^11.23.0”
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:20 (5 by maintainers)
Top Results From Across the Web
React Native Webview - injectJavascript not working
I'm trying to inject JS code to alert the user before the content of the React Native Webview loads, however, for me it...
Read more >WebView · React Native
goBack(). goBack();. Go back one page in the web view's history. reload().
Read more >Rendering a web content in native view using WebView API in ...
Basically WebView renders web content in a native view, ... WebView from "react-native-webview";const App = () => {const [canGoBack, ...
Read more >Build web apps in WebView - Android Developers
It does not include any features of a fully developed web browser, ... To load a web page in the WebView , use...
Read more >docs/Reference.md · Mar-er/react-native-webview - Gitee.com
... goForward(); goBack(); reload(); stopLoading(); injectJavaScript(str); requestFocus(); clearFormData(); clearCache(bool); clearHistory(). Other Docs ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That problem happens for the react native webview version you are on… Kindly downgrade the version react-native-webview version: “^11.23.0” to react-native-webview version: “^11.18.1”
I think their upgrade broke a code… I also had similar issue
Pretty sad how bad things are maintained in the react native ecosystem. Everyone is always just looking for workarounds instead of fixes because the maintainers don’t seem to care.