我正在为混合应用程序开发早期原型,最终它将使用更多的React Native组件,但此时它只是一个WebView。
我使用Facebook的文档中的示例代码,但似乎window.location
WebView内的JS更新不会触发onNavigationStateChange回调。
我的渲染功能中有组件:
<WebView
ref={WEBVIEW_REF}
automaticallyAdjustContentInsets={false}
style={styles.webView}
url={this.state.url}
javaScriptEnabled={true}
domStorageEnabled={true}
onNavigationStateChange={this.onNavigationStateChange}
onShouldStartLoadWithRequest={this.onShouldStartLoadWithRequest}
startInLoadingState={true}
scalesPageToFit={this.state.scalesPageToFit}
/>
更新导航更改状态的方法:
onNavigationStateChange: function(navState) {
this.setState({
backButtonEnabled: navState.canGoBack,
forwardButtonEnabled: navState.canGoForward,
url: navState.url,
status: navState.title,
loading: navState.loading,
scalesPageToFit: true
});
}
我正在加载的页面尝试window.location.replace()
或直接分配window.location.href
:
if (window.location.replace){
window.location.replace(url);
} else {
window.location.href = url;
}
我看到this thread表示window.location.replace在Android中不起作用,但window.location.href
作业不应该通过吗?当我在onNavigationStateChange方法中设置断点时,它永远不会收到与更新的URL相对应的navState,这意味着WebView永远不会重定向,它只是位于最初加载的URL。
我可以尝试将其重新设计为不需要重定向,但它来自我无法控制的网站(Salesforce),因此我希望在React Native组件中找到解决方案。任何建议都会非常有用!