Alessandro Solbiati
Alessandro Solbiati

Reputation: 979

react-native Linking.getInitialURL doesn't return query parameters

Following the documentation you can use the method Linking.getInitialURL to get which URL the app was opened with (e.g. if opened from a web browser) as follow:

// called from exp://expo.io/@solbiatialessandro/GettingIntoYc
export default function App(props) {
  Linking.getInitialURL().then((url) => {
    if (url) {
      console.log('Initial url is: ' + url);
      // 'Initial url is exp://expo.io/@solbiatialessandro/GettingIntoYc'
    }
  }).catch(err => console.error('An error occurred', err));
  return (<AppNavigator />);
}

The above works correctly, however I can't manage to retrieve query parameters added to the url using the snippet below

// called with exp://expo.io/@solbiatialessandro/GettingIntoYc?test=1234
export default function App(props) {
  Linking.getInitialURL().then((url) => {
    if (url) {
      let { path, queryParams } = Linking.parse(url);
      // path: exp://expo.io/@solbiatialessandro/GettingIntoYc
      // queryParams: null
    }
  }).catch(err => console.error('An error occurred', err));
  return (<AppNavigator />);
}

Moreover if I try to console.log(url) I get the input url without the ?test=1234, so it looks like is getting discarded by the Linking module.

Upvotes: 0

Views: 3411

Answers (2)

Hans
Hans

Reputation: 21

I had a solution which worked for me:

Instead of calling with 'exp://expo.io/@solbiatialessandro/GettingIntoYc?test=1234', perhaps you can try calling it with "exp://expo.io/@solbiatialessandro/GettingIntoYc/index.exp?test=1234"

Note: The link i tried with redirected me to an Expo app (SDK 37.0.0) that was published (not built). Also, just merely using 'exp://expo.io/@solbiatialessandro/GettingIntoYc?test=1234' works when tested with an IOS device, however it could not work with an Android device.

hope this helps!

Upvotes: 2

SG Prasanth
SG Prasanth

Reputation: 69

Its weird but this worked for me

let newURL = Linking.parse(url);
console.log(newURL.queryParams);

Upvotes: 0

Related Questions