After enabling deep linking, everything seems to be working fine when the application is opened. However, I've encountered an issue when trying to open the app from a background state using the URL moderatorapp://hello. While it logs the correct URL, the deep linking functionality does not work in this scenario. Below is the code snippet that I am using:
componentDidMount() {
// Storage.clear();
Storage.getItem('data_moderator')
.then(_data => {
if (_data && _data.tokens) {
this.autoLogin(_data.tokens);
} else {
Actions.loginForm();
}
}
);
Linking.getInitialURL()
.then(url => {
console.log('Initial Url then ', url);
if (url) {
console.log('Initial Url ', url);
}
})
.catch(error => console.log(error));
Linking.addEventListener('url', this.handleOpenURL);
}
The issue seems to be because the componentDidMount method is not being called at that particular point.
Approaches Tried:
I tried wrapping the Linking code inside an event listener that detects when the application enters the active state, but it did not resolve the issue. It still logs the same URL as the initial attempt when the app was closed. Additionally, when attempting to deep link into the app from the background state using the URL moderatorapp://goodbye, it logs the moderatorapp://hello instead of updating as expected.
AppState.addEventListener('change', (state) => {
if (state === 'active') {
console.log('state active');
Linking.getInitialURL()
.then(url => {
console.log('Initial Url then ', url);
if (url) {
console.log('Initial Url ', url);
}
})
.catch(error => console.log(error));
}
if(state === 'background'){
console.log('background');
}
});
As a newcomer to React Native, any guidance on how to address this issue would be highly appreciated.
Thank you.