Webview组件允许我指定目标url,例如facebook.通用域名格式

render() {

    return (
        <WebView
          source={{uri: https://www.facebook.com}}
          style={{marginTop: 20}}
        />
    );
  }

然而,如果我点击facebook上的链接,我如何才能得到被点击的url或被登陆的url?

推荐答案

这是我在上一个项目中使用的Webview.

<WebView
       ref="webview"
       source={{uri:this.state.url}}
       onNavigationStateChange={this._onNavigationStateChange.bind(this)}
       javaScriptEnabled = {true}
       domStorageEnabled = {true}
       injectedJavaScript = {this.state.cookie}
       startInLoadingState={false}
     />

这句话对你来说至关重要:

           onNavigationStateChange={this._onNavigationStateChange.bind(this)}

你可以这样读网址:

_onNavigationStateChange(webViewState){
  console.log(webViewState.url)
}

如果我没记错的话,在加载url时会触发3次.

webviewState对象原型:

{
  canGoBack: bool,
  canGoForward: bool,
  loading: bool,
  target: number,
  title: string,
  url: string,
}

第三次(最后一次)调用此事件时,加载属性为false

如果有帮助,请告诉我.

React-native相关问答推荐

expo 使用错误的版本代码创建建筑

React-Native:请纠正我对状态如何工作的误解 - 动态文本输入

@testing-library/react-native -> 渲染 -> TypeError:无法读取未定义的属性(读取存在)

使用下拉菜单时react 本机react-native-dropdown-select-list,它会将下面的项目向下移动.如何阻止这种情况发生

防止 React Native 在上下文值更改时重新安装组件

为什么当键盘出现在react native 时我的按钮会上升?按钮视图位置是绝对的?

React Native Axios 通过数组的键过滤

如何清除react-native webview cookie?

使用直接在 Windows 中运行的模拟器在 WSL 中运行 React Native

如何从 React Native 应用程序打开 Google Play store ?

React Native:无法点击调试菜单

java.lang.UnsatisfiedLinkError:找不到要加载的 DSO:libfbjni.so 结果:0

如何在 React Native 中获取电话号码?

检测是否通过推送通知打开了 React Native iOS 应用

在 React 中调用 getDerivedStateFromProps 中的方法

ListView 和 FlatList 有什么区别?

react-native:多色文本视图

Android 是否支持 React Native 的 LayoutAnimation?

在 React Native 中向组件传递参数

NavigationActions.reset 不是函数吗?