admin管理员组文章数量:1328972
I am having URL as
I need to fetch value of code. Below is my code:
import React from 'react';
import { useEffect } from 'react';
const Callback = () => {
useEffect(() => {
console.log("hiii");
}, []);
return (
<React.Fragment>
Callback
</React.Fragment>
);
};
export default Callback;
Please confirm how can I fetch that. Thanks in advance.
I am having URL as http://example./callback?code=abcd
I need to fetch value of code. Below is my code:
import React from 'react';
import { useEffect } from 'react';
const Callback = () => {
useEffect(() => {
console.log("hiii");
}, []);
return (
<React.Fragment>
Callback
</React.Fragment>
);
};
export default Callback;
Please confirm how can I fetch that. Thanks in advance.
Share Improve this question edited Oct 1, 2020 at 11:12 norbitrial 15.2k10 gold badges39 silver badges64 bronze badges asked Oct 1, 2020 at 9:50 Dilpreet KaurDilpreet Kaur 1074 silver badges10 bronze badges 1- your link not found – Anhdevit Commented Oct 1, 2020 at 10:07
5 Answers
Reset to default 3Probably the best way is to use the useParams
hook because you have a functional ponent. Read from the documentation as the following:
useParams
returns an object of key/value pairs of URL parameters. Use it to accessmatch.params
of the current<Route>
.
I would suggest the following:
import React from 'react'
import { useParams } from 'react-router-dom'
const Callback = () => {
let { code } = useParams()
console.log({ code });
return (
<React.Fragment>
Callback
</React.Fragment>
)
};
+1 from documentation:
You need to be using React >= 16.8 in order to use any of these hooks!
use this library 'query-string' and use as
import queryString from 'query-string';
in constructor
constructor(props) {
const { location: { search } } = props;
const parsed = queryString.parse(search);
this.state = {
code: parsed.code,
}
}
hope it will solve your problem !
You can use the URLSearchParams interface to fetch the query string of a URL. https://developer.mozilla/en-US/docs/Web/API/URLSearchParams
const queryParams = new URLSearchParams(window.location.search);
const code = queryParams.get("code");
First at App.js you should have react-router-dom
import { Route, Switch } from "react-router-dom";
Then inside switch you can define route parameters like in your case
<Route exact path="/callback/:code" ponent={YourComponentsName} />
At the ponents file you can get the parameters from the props
Functional Component
const code = props.match.params.code
Class Component
const code = this.props.match.params.code
Or next approach
const code = new URLSearchParams(this.props.location.search);
const code = query.get('code')
console.log(code)//abcd
You need to use a hook...
useEffect(() => {
let urlParams = new URLSearchParams(window.location.search);
console.log(urlParams.code || "empty");
}, []);
本文标签: javascriptGet query string value in React JSStack Overflow
版权声明:本文标题:javascript - Get query string value in React JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742248775a2440355.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论