react-native-url-polyfill icon indicating copy to clipboard operation
react-native-url-polyfill copied to clipboard

Parsing search params

Open ap1969 opened this issue 3 years ago • 1 comments

Hi, First, thanks for putting this out there.

That said, I've got an issue that I think must be me, because it's such an obvious one:

  const myURL = new URL('https://example.org/abc?foo=bar');
  console.log("Search", myURL.search);
  console.log("Params ", myURL.searchParams);

results in the following output:

Search ?foo=bar
Params  {}

Again - that's such an obvious miss, that it must be me, but I can't figure out why!

ap1969 avatar Aug 16 '22 20:08 ap1969

My pleasure, thanks for reaching out!

URL.searchParams returns a URLSearchParams object, from there you can iterate over params with forEach(), entries(), keys(), or values().

Note: URL.searchParams returns a URLSearchParams, so the empty object you're seeing in the output is the URLSearchParams not the dictionary of params.

In your case, it seems like you would like to display params as a string, so you could use searchParams.toString() or as an object with Object.fromEntries(searchParams.entries()).

Or, if you want a specific key: searchParams.get('foo').

Let me know if something isn't working as expected, I tested this in Chrome directly.

charpeni avatar Aug 16 '22 20:08 charpeni

Closing this for now, please let me know if the polyfill isn't working as expected.

charpeni avatar Aug 19 '22 00:08 charpeni