window.location 更新不会触发 onNavigationStateChange?

Kyl*_*ell 5 android-webview react-native

我正在开发混合应用程序的早期原型,最终它将使用更多的 React Native 组件,但目前它只是一个 WebView。

我使用的是 Facebook 文档中的示例代码,但window.locationWebView 内的 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}
/>
Run Code Online (Sandbox Code Playgroud)

以及更新导航更改状态的方法:

onNavigationStateChange: function(navState) {
  this.setState({
    backButtonEnabled: navState.canGoBack,
    forwardButtonEnabled: navState.canGoForward,
    url: navState.url,
    status: navState.title,
    loading: navState.loading,
    scalesPageToFit: true
  });
}
Run Code Online (Sandbox Code Playgroud)

我正在加载的页面尝试window.location.replace()window.location.href直接分配:

if (window.location.replace){ 
  window.location.replace(url);
} else {
  window.location.href = url;
} 
Run Code Online (Sandbox Code Playgroud)

我看到这个线程说 window.location.replace 在 Android 中不起作用,但是window.location.href分配不应该通过吗?当我在 onNavigationStateChange 方法中设置断点时,它永远不会收到与更新的 URL 相对应的 navState,这意味着 WebView 永远不会重定向,它只是位于最初加载的 URL 处。

我可以尝试对此进行修改,以不需要重定向,但它来自我无法控制的站点(Salesforce),因此我希望在 React Native 组件中找到解决方案。任何建议都会非常有帮助!