问题描述
我想在React本机Webview中打开一个网页,并在默认浏览器中打开hrefs(链接)。我尝试使用Webview的stopLoading方法,但是它冻结了视图。我使用此线程上的答案成功实现了这一目标。
The method stopLoading of react-native-webview causes the website to freeze
但是在某些情况下,随机地,单击href链接后,除了在默认浏览器中打开网页外,它还会在Webview中打开网页。我希望仅在浏览器中将其打开。请在这里检查我在做什么错。还是有更好的方法来实现这一目标?
这是我正在使用的代码:
LoadingIndicatorView() {
return (
<ActivityIndicator
color="#009b88"
size="large"
style={{ flex: 1,justifyContent :'center',}}
/>
);
}
handleWebViewRequest = request => {
const {url} = request;
Linking.openURL(url);
return false;
}
render() {
let uri = 'https://www.google.com/';
let sku = this.props.route.params.sku;
let location = this.props.route.params.location;
return (
<WebView
ref={ref => (this.webview = ref)}
source={{ uri: uri,method: 'POST',body: 'device=tablet&search='+sku+'&ref='+location }}
renderLoading={this.LoadingIndicatorView}
startInLoadingState={true}
onShouldStartLoadWithRequest={this.handleWebViewRequest.bind(this)}
style={{ flex: 1 }}
/>
);
}
解决方法
我在为 onShouldStartLoadWithRequest
调用的方法中添加了 stopLoading
webViewRef.current.stopLoading();
其中 webViewRef 是 webView 的引用
ref = { webViewRef }