I am trying to extract the code from an incoming link in react native. I am able to receive the url and print it in my console but having trouble getting the param from url.
My code:
useEffect(() => {
Linking.addEventListener('url', callback);
})
callback = async (url) => {
if (url !== null) {
const new_url = JSON.stringify(url)
console.log(new_url)
const urlCallback = new URL(new_url);
const code = urlCallback.searchParams.get('code');
console.log(code)
}
};
I keep getting this error: TypeError: Invalid URL: {"url":"myapp://?code=ABCDEFG"}
I am trying to query out that code so i can send it as a param in a post request.
Appreciate any help, thanks!
CodePudding user response:
Based on the error message, it seems you're stringifying url object, not string. Try to parse from its 'url' prop instead.
callback = async (url) => {
if (url !== null) {
const new_url = url.url;
console.log(new_url)
const urlCallback = new URL(new_url);
const code = urlCallback.searchParams.get('code');
console.log(code)
}
};
CodePudding user response:
Try this query-string
const parsed = queryString.parseUrl("myapp://?code=ABCDEFG");
console.log(parsed.query.code)