Kokab Shakil
Kokab Shakil

Reputation: 117

how to get query string params from api in react Native

https://obikes.page.link/d6o5/?ref=10959bc I am using axios this is my invite link in my app i want to get data after query string i need ref code ref=10959bc ,how can i get this query data 10959bc in react native i am unable to find any solution

React.useEffect(async () => {
    const getValue = await AsyncStorage.getItem('token');
    
    await getReferal().then(response => {
      console.log(response.data.refferalUrl); //https://obikes.page.link/d6o5/?ref=10959bc
      //  refer code 
  
    })
    .catch(error => {
      console.log(error);
    });

Upvotes: 0

Views: 232

Answers (2)

Quang Thái
Quang Thái

Reputation: 697

A pure JS approach:

React.useEffect(async () => {
    const getValue = await AsyncStorage.getItem('token');
    
    await getReferal().then(response => {
        console.log(response.data.refferalUrl);
        // refer code:
        const url = response.data.refferalUrl
        let regex = /[?&]([^=#]+)=([^&#]*)/g, params = {}, match;
        while ((match = regex.exec(url))) {
            params[match[1]] = match[2];
        }
        console.log(params) // => {ref: "10959bc"}  
    })
    .catch(error => {
      console.log(error);
    });

Upvotes: 1

Jignesh Mayani
Jignesh Mayani

Reputation: 7193

Use the qs npm package to get the query string params from a string.

https://github.com/ljharb/qs

Upvotes: 0

Related Questions