question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Webview not responding to goBack() or injectJavascript()

See original GitHub issue

Hello, 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:open
  • Created a year ago
  • Reactions:3
  • Comments:20 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
iambigmarncommented, Aug 16, 2022

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

2reactions
lennartkloockcommented, Nov 11, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found