react router dom query params code example
Example 1: react router url params
import { Route, useParams } from "react-router-dom";
<Route path="/:id" children={<Child />} />
let { id } = useParams();
Example 2: use query params react
const query = new URLSearchParams(this.props.location.search);
const token = query.get('token')
console.log(token)
Example 3: get query params react
new URLSearchParams(this.props.location.search).get("your_query_param_key")
Example 4: react router dom useparams
import React from "react";
import ReactDOM from "react-dom";
import {
BrowserRouter as Router,
Switch,
Route,
useParams
} from "react-router-dom";
function BlogPost() {
let { slug } = useParams();
return <div>Now showing post {slug}</div>;
}
ReactDOM.render(
<Router>
<Switch>
<Route exact path="/">
<HomePage />
</Route>
<Route path="/blog/:slug">
<BlogPost />
</Route>
</Switch>
</Router>,
node
);