Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native webview get url

Webview component allowing me to specify a destination url, e.g. facebook.com

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

However, If i click the link in facebook, how can I get the url being clicked or the url being landed?

like image 637
bns Avatar asked Aug 23 '16 11:08

bns


People also ask

How do I get my Webview URL in React Native?

To get current URL with React Native webview, we can set the onNavigationStateChange prop to a function that listens for navigation events in the webview. to set onNavigationStateChange to the onNavigationStateChange function. In it, we log webViewState. url which has the URL of the current page in the webview.

How do I get data from React Native in Webview?

Create a function called webviewRef and attach it to the WebView component. Next, create a function called sendDataToWebView and add the code below to it. Inside the “script” tag, use window. addEventListener to listen to the event from the React Native app.

Can I use Webview in React Native?

WebViews offer developers opportunities to render any web components in a React Native application. A web component can be anything from a whole webpage/application or just a simple HTML file. The package react-native-webview makes it super simple to embed WebViews into your React Native apps!

What is Webview in React Native?

A WebView is an embedded browser that can be used to display web pages inside your React Native applications. It can display anything (from custom HTML elements to entire web applications) inside React Native. In React Native, we can use WebView by using a third-party package called react-native-webview.


1 Answers

Here is a Webview I used for my previous project.

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

for you essential is this line:

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

and you can read the URL like this:

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

If I remember correctly this fires 3 times when loading a url.

webviewState object prototype:

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

The 3rd (final) time this event is called, the loading attribute is false

let me know if it helps.

like image 94
dv3 Avatar answered Oct 04 '22 07:10

dv3