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.

onNavigationStateChange is not working on Android in Expo App

See original GitHub issue

Hey, in the current Version “react-native-webview”: “8.1.1” the onNavigationStateChange event is not triggered in any Android device. On iOS it works perfectly. It is no normal # tag url change, it is a real change from /home to /dashboard.

import React, {
  useEffect,
  useState,
  useRef,
  useCallback,
  StyleSheet,
  useContext,
} from "react";
import {
  RefreshControl,
  ActivityIndicator,
  View,
  ScrollView,
  Alert,
} from "react-native";
import { WebView } from "react-native-webview";

const WebViewComponent = (props) => {
  const [refreshing, setRefreshing] = useState(false);
  const [loaded, isLoaded] = useState(false);
  const [urlReal, setUrl] = useState((props?.url != undefined
        ? props.url
        : "httsp://google.com)
  );

  const webViewRef = useRef();

  const onRefresh = useCallback(async () => {
    setRefreshing(true);
    webViewRef.current.reload();
    setRefreshing(false);
  }, [refreshing]);

  const _onNavigationStateChange = (webViewState) => {
    console.log(webViewState.url);
    // no action on android
  };

  return (
    <View>
      <ScrollView
        refreshControl={
          <RefreshControl refreshing={refreshing} onRefresh={onRefresh} />
        }
      >
        <WebView
          ref={(ref) => (webViewRef.current = ref)}
          onLoadStart={(e) => {
            console.log("onLoadStart");
          }}
          onLoad={(e) => {
            console.log("onLoad");
          }}
          onLoadEnd={(e) => {
            console.log("onLoadEnd");
          }}
          renderError={(e) => {
            console.log("renderError");
          }}
          source={{
            uri: urlReal,
          }}
          startInLoadingState={true}
          domStorageEnabled={true}
          javaScriptEnabled={true}
          onNavigationStateChange={(e) => _onNavigationStateChange(e)}
          thirdPartyCookiesEnabled={true}
        />
      </ScrollView>
    </View>
  );
};

Environment:

  • OS: Andriod
  • expo version: 37.0.3
  • react-native-webview version: 8.1.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:18

github_iconTop GitHub Comments

7reactions
jkoutavascommented, Oct 2, 2020

This issue still exists. I’m seeing it on React Native 0.61.0 and react-native-webview 10.9.1

5reactions
mobihackcommented, Jul 27, 2020

Possible fix is to call updateNavigationState when webview url is changed.

That was for an internal fix.

Check below link for an idea. You could find some way to send route change in the SPA to the react native application.

https://stackoverflow.com/questions/53297300/how-to-communcation-between-web-inside-webview-back-to-react-native-apps

Read more comments on GitHub >

github_iconTop Results From Across the Web

onNavigationStateChange not working in Android React Native
Based on your comment I understand that you want to have a WebView in your react native app and inside there to load...
Read more >
onNavigationStateChange is not working on Android in Expo ...
Hey, in the current Version "react-native-webview": "8.1.1" the onNavigationStateChange event is not triggered in any Android device.
Read more >
onNavigationStateChange of WebView on android get url with ...
Changing source to uri from url was my fix. This was only happening to me on Android, onNavigationStateChange was returning url "about:blank". Cheers:...
Read more >
Monitor your React Native application
With our React Native agent, your team can monitor the performance of its hybrid apps and identify code errors. Our agent collects crash...
Read more >
react-native-webview - npm
React Native WebView component for iOS, Android, macOS, and Windows. ... Start using react-native-webview in your project by running `npm i ...
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