反应原生 WebView 导航

时间:2021-01-14 17:20:48

标签: react-native

我正在使用

const newURL = 'https://www.google.com/';
const redirectTo = 'window.location = "' + newURL + '"';
this.webview.injectJavaScript(redirectTo);

导航。

有没有其他方法可以做到这一点,因为每次我执行 window.location 时我的网络应用程序都会重新加载,并且网络应用程序状态消失了。

亲切的问候!

1 个答案:

答案 0 :(得分:1)

在类组件的构造函数方法中,为 url 添加一个新属性来声明。

  constructor(props) {
    super(props);
    this.state = { url: "https://www.google.com/" };
  }

然后更新您的 webview 源以使用新的状态变量作为源。

<WebView source={{ uri: this.state.url }} />
<Button onPress={this.buttonPressEvent} />

然后每当您想更改网址时,

  buttonPressEvent = (event) => {
    //set new url here.
    this.setState({ url: 'https://facebook.com' });
  }
相关问题