I am trying to get URL params in a React Native app.
What I tried to do:
- fails because searchParams is an empty list
const parsedUrl = new URL(url)
// here the searchParams are empty list
console.log(parsedUrl.searchParams)
- fails because the following error is shown:
[Error: not implemented]
const parsedUrl = new URLSearchParams(url)
console.log(parsedUrl.get(param))
MaartenDev
5,8315 gold badges23 silver badges36 bronze badges
asked Aug 16, 2021 at 15:18
spatak
1,4895 gold badges19 silver badges32 bronze badges
-
Could you provide an example url that contains values you want to acess?MaartenDev– MaartenDev2021年08月16日 15:22:23 +00:00Commented Aug 16, 2021 at 15:22
-
myapp://?code=123123-1123123-418c-33213-123123spatak– spatak2021年08月16日 15:47:56 +00:00Commented Aug 16, 2021 at 15:47
-
You should look into deep linking: reactnavigation.org/docs/deep-linking that is the way urls work on mobile devices and appsMaartenDev– MaartenDev2021年08月16日 18:20:17 +00:00Commented Aug 16, 2021 at 18:20
4 Answers 4
You Can Try this:
( Only For React Js )
function getURLParams(parameterName, url) {
let name = parameterName.replace(/[\[\]]/g, '\\$&');
let regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'), results = regex.exec(url);
if (!results) return null;
if (!results[2]) return null;
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
// For Current Window URL
console.log(getURLParams("param1", window.location.href));
// For Custom URL
console.log(getURLParams("param1", "https://example.com/index.html?param1=Hello"));
Or This
( For React Native )
var url = "http://example.com?param1=test¶m2=someData&number=123"
var regex = /[?&]([^=#]+)=([^&#]*)/g,
params = {},
match;
while (match = regex.exec(url)) {
params[match[1]] = match[2];
}
console.log(params)
answered Aug 16, 2021 at 15:31
Aditya
1,3592 gold badges16 silver badges37 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
const getSearchParamFromURL = (url, param) => {
const include = url.includes(param)
if (!include) return null
const params = url.split(/([&,?,=])/)
const index = params.indexOf(param)
const value = params[index + 2]
return value
}
const _url = 'myapp://?code=123123-1123123-418c-33213-123123¶m_key=param_value'
getSearchParamFromURL(_url, 'code') // output: 123123-1123123-418c-33213-123123
answered Aug 16, 2021 at 15:46
spatak
1,4895 gold badges19 silver badges32 bronze badges
1 Comment
MaartenDev
This seems to solve the wrong problem, the question is about retrieving the parameter value not parsing the parameter value
This is how I do in my custom Link.tsx component in react-navigation 5:
export const Link = function Link({ to, action, target, onPress, children, params }: Props) {
const searchParams = new URLSearchParams(params as Record<string, string>)
return (
<DefaultLink
to={`${to.startsWith('/') ? to : `/${to}`}${params ? `?${searchParams.toString()}` : ''}`}
action={action}
target={target}
onPress={onPress}>
{children}
</DefaultLink>
)
}
If you are also using react-navigation, you can use pathConfig.parse and pathConfig.stringify to automatically convert the typing. getStateFromPath|getPathFromState
answered Aug 16, 2021 at 15:22
Dimitri Kopriwa
14.7k35 gold badges119 silver badges236 bronze badges
Comments
This is the function I use (typescript). It returns an object of { key: value }.
export const getUrlParams = (url: string) => {
const params: Record<string, string> = {};
const queryString = url.split('?')[1];
if (!queryString) {
return params;
}
queryString.split('&').forEach((param) => {
const [key, value] = param.split('=');
if (key) {
params[decodeURIComponent(key)] = decodeURIComponent(value || '');
}
});
return params;
};
Comments
lang-js