React Native - Parse URL to get request variable

Looking for a way to parse the url to get the request variable in React Native obtained from Linking.

I am getting the url something like:

Application url schema: // somePage someVar = someVal

I want to get someVar value from url.

Any thoughts?

+3


source to share


4 answers


There are ways to do this that you can use from the JS ecosystem. Try URI.js https://github.com/medialize/URI.js



+2


source


This should do the trick



var url = "http://example.com?myVar=test&otherVariable=someData&number=123"

var regex = /[?&]([^=#]+)=([^&#]*)/g,
  params = {},
  match;
while (match = regex.exec(url)) {
  params[match[1]] = match[2];
}
console.log(params)
      

Run codeHide result


+2


source


Try url https://www.npmjs.com/package/url

"This module has url resolution and parsing utilities to enforce function parity using the node.js core url module."

The URL node object is available to your app when using the responsive Chrome debugger, but not available on your iPhone when not tied.

So use this package

npm install url

      

Sample code:

import url from 'url';

...

let urlObject = url.parse(inUrlString);
let outUrlString = urlObject.protocol + '//' + urlObject.host + '/more/jump?jump_path=' + encodeURIComponent(urlObject.pathname);

      

+1


source


Update. This is not a solution to the above question because it doesn't work unless in debug mode. However, I did not delete this answer because I think it points to a note worthy of a nuance.

JavaScript very native URL Web API is supported in React Native (using version 0.46). You can use to parse or assemble any part of the URL with great ease.

The API is identical to the WHATWG API URL Module API in Node.js This answer should be more obvious, but easy to get lost with the number of URL parsing packages available.

Edit: This only works in JS debug mode for some reason, not otherwise. So this solution is really not valid, but I am leaving it here because I would like to know how to get the same url module as to work with native-native.

0


source







All Articles