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: Unable to prevent navigation on android, working on iOS

See original GitHub issue

Bug description:

I’m trying to intercept a navigation change. On iOS, this works correctly where ref.current.stopLoading() prevents the WebView from navigating. On Android, it goes ahead and navigates (if at least partially) before stopLoading() takes effect.

To Reproduce:

I’ve made a fresh repo that exhibits this behaviour: https://github.com/alancwoo/react-native-webview-iss-1625

const App: () => React$Node = () => {
  const [url, setUrl] = useState('https://github.com/react-native-community/react-native-webview')
  const [key, setKey] = useState(0)

  const webViewRef = useRef(null)
  return (
    <>
      <StatusBar barStyle="dark-content" />
      <SafeAreaView style={{ flex: 1, width: '100%' }}>
        <WebView
          key={key}
          ref={webViewRef}
          source={{ uri: url }}
          style={{ flex: 1, width: '100%' }}
          onNavigationStateChange={(event) => {
            if (event.url !== url) {
              webViewRef.current.stopLoading()
              Alert.alert('stopped')
            }
          }}
        />
      </SafeAreaView>
    </>
  );
};

Expected behavior:

Android should behave the same as on iOS, and should halt loading before transitioning.

Screenshots/Videos:

Android Start: image

Tap on ‘Sign Up’ (Loads signup page before stopping and showing alert): image

iOS (Correctly stops loading and shows alert): image

Environment:

  • OS: Mac OS
  • OS version: 10.15.6
  • react-native version: 0.63.2
  • react-native-webview version: ^10.8.3

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:24

github_iconTop GitHub Comments

9reactions
DevAhmad7commented, Feb 19, 2021

work with me

onLoadProgress={(e) => {
          const state = e.nativeEvent;
          setCanGoBack(state.canGoBack)
}}

Screenshot (168)

6reactions
ausshaducommented, Feb 5, 2021

@alancwoo

react-native-webview: ^11.2.1 react-native: 0.63.4

I tried using onShouldStartLoadWithRequest didn’t work 😕. No matter what, the page always redirects. The issue is only on Android. iOS works as expected.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to prevent redirecting when using react-native-webview ...
Is it possible way to do this? In IOS, using stopLoading works fine but it is not working on Android, onNavigationStateChanged(navState) { if( ......
Read more >
React Native WebView: A complete guide - LogRocket Blog
This package will work both on Android and iOS devices. Here is a short overview of the terminal commands: # cd into the...
Read more >
How to handle navigation with WebViews in a React Native app
We're going to use an iOS simulator for this tutorial. If you're on Windows or Linux based operating systems, you can use Android...
Read more >
WebView - React Native
You can use this component to navigate back and forth in the web view's history and configure various properties for the web content....
Read more >
How to use WebView in React Native - Educative.io
Create a React Native project · Add dependencies · Link native dependencies · For iOS and macOS · For Android · Implementing a...
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