Search code examples
react-nativeexporeact-native-iosreact-native-webview

react-native-webview why is the goBack() method not working?


I have a simple React Native project in Expo, it launches a website using react-native-webview.

Here is the source code:

import React from "react";
import { StyleSheet, View, SafeAreaView } from "react-native";
import { AntDesign } from "@expo/vector-icons";

import { WebView } from "react-native-webview";


export default function App() {
  const goback = () => {
    WebView.goBack();
  };

  return (
    <SafeAreaView>
      <WebView source={{ uri: "https://google.co.uk" }} />
      <View style={styles.navbar}>
        <View style={styles.forward}>
          <AntDesign name="right" size={25} color="grey" />
        </View>
        <View style={styles.back}>
          <AntDesign name="left" size={25} color="grey" onPress={goback} />
        </View>
      </View>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  navbar: {
    height: 40,
    width: "100%",
    flexDirection: "row-reverse",
    paddingTop: 6,
    backgroundColor: "#fefefe",
    borderTopColor: "grey",
    borderTopWidth: 1,
  },
  back: {
    width: 50,
    height: 50,
    marginRight: 10,
  },
  forward: {
    width: 50,
    height: 50,
  },
});

The WebView component loads the website fine (google.co.uk) but I can not get the navigation to work. I simply want to create a back button, that allows the user to navigate back to other pages they have viewed in the WebView, and forward if they have gone back and want to go forward.

For now I am trying to get the back button working. I load the app, then navigate to a different page on the WebView. When the back button is pressed, the following error is generated:

TypeError: _reactNativeWebview.WebView.goBack is not a function (In '_reactNativeWebview.WebView.goBack()','_reactNativeWebview.WebView.goBack' is undefined)

According to the doc's the goBack() method exists:

goBack()

I found this but it is implementing a class based component so I couldn't easily map the suggestions into my functional component, and further, I think that solution is overkill as they are intercepting the navigation, I believe what I am trying to achieve should be simpler, but I can't get the basic navigation to work on the WebView (i.e. go back and forward to previously viewed pages).


Solution

  • Everything mentioned by you is correct Gary. The only thing that you need to change is the way how goBack function is called. goBack is not a component's direct function rather you need to pass on a reference to the WebView component to get this function. In your case you can change your component as below to get this working:-

    import React, { useRef } from "react";
    import { StyleSheet, View, SafeAreaView } from "react-native";
    import { AntDesign } from "@expo/vector-icons";
    
    import { WebView } from "react-native-webview";
    
    
    export default function App() {
      const webViewRef = useRef(null)
      
      const goback = () => {
        webViewRef.current.goBack();
      };
    
      return (
        <SafeAreaView>
          <WebView ref={webViewRef} source={{ uri: "https://google.co.uk" }} />
          <View style={styles.navbar}>
            <View style={styles.forward}>
              <AntDesign name="right" size={25} color="grey" />
            </View>
            <View style={styles.back}>
              <AntDesign name="left" size={25} color="grey" onPress={goback} />
            </View>
          </View>
        </SafeAreaView>
      );
    }
    
    const styles = StyleSheet.create({
      navbar: {
        height: 40,
        width: "100%",
        flexDirection: "row-reverse",
        paddingTop: 6,
        backgroundColor: "#fefefe",
        borderTopColor: "grey",
        borderTopWidth: 1,
      },
      back: {
        width: 50,
        height: 50,
        marginRight: 10,
      },
      forward: {
        width: 50,
        height: 50,
      },
    });
    

    This refernce will help you in calling any reference functions mentioned in the documentation of webview module. Enjoy!