I use linking for opening my app from the browser.
When I tap the link for first in-browser my app run but incoming URL don't clear after that and linking.GetInitialUrl()
always run with that URL.
My schema is myapp://host
and my URL on the web is myapp://host/ok
I click on my URL and linking.getInitialUrl()
works but when next time I'm back to My screen linking.getInitialUrl()
return my web URL without open web page by me.
componentDidMount() {
Linking.getInitialURL().then(url => {
if (url) {
alert(url)
}
})
.catch(err => {
console.error(err);
});
Linking.addEventListener('url',this.handleOpenURL);
}
componentWillUnmount() { Linking.removeEventListener('url',this.handleOpenURL);
}
handleOpenURL = (event) => { // D
this.linkFunc(event.url);
}
There are two ways to handle Deep Linking in a React Native app: Without navigation: by invoking React Native's core library via JavaScript and directly calling Linking . You can learn more about this in React Native's official documentation. With navigation: by configuring React Navigation library.
Since you are calling getInitialURL
method in componentDidMount
method without checking if the app is already loaded or not. Your alert(url)
will get triggered whenever that component is loaded again.
To solve the problem, you have to call getInitialURL
in the root component that will never be loaded again after the app is loaded.Or you can use a global variable to mark the status of your app whether it is already loaded or not.
if(!InMemoryData.appLoaded){
Linking.getInitialURL().then(url => {
this._navigate(url);
InMemoryData.appLoaded = true;
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With