react-native-url-polyfill
react-native-url-polyfill copied to clipboard
Parsing search params
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!
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.searchParamsreturns aURLSearchParams, so the empty object you're seeing in the output is theURLSearchParamsnot 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.
Closing this for now, please let me know if the polyfill isn't working as expected.