onNavigationStateChange: Unable to prevent navigation on android, working on iOS
See original GitHub issueBug 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:
Tap on ‘Sign Up’ (Loads signup page before stopping and showing alert):
iOS (Correctly stops loading and shows alert):
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:
- Created 3 years ago
- Reactions:8
- Comments:24
Top 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 >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
work with me
@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.