Simon Giesen
Simon Giesen

Reputation: 377

React Native WebView Navigation

I am using

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

to navigate.

Is there any other way to do this since my web app is reloading every time I do a window.location and web app state is vanished.

Kind regards!

Upvotes: 0

Views: 902

Answers (1)

emkarachchi
emkarachchi

Reputation: 802

Inside the constructor method inside your class component, add a new property to state for url.

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

Then update your webview source to use the new state variable as the source.

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

Then whenever you want to change the url,

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

Upvotes: 1

Related Questions