1import { useLocation } from 'react-router-dom'
2
3// Location is, for example: http://localhost:3000/users/new
4
5// Care! MyComponent must be inside Router to work
6const MyComponent = () => {
7 const location = useLocation()
8
9 // location.pathname is '/users/new'
10 return <span>Path is: {location.pathname}</span>
11}
12
13export default MyComponent
1import {
2 BrowserRouter as Router,
3 Switch,
4 Route,
5 useParams
6} from "react-router-dom";
7
8function BlogPost() {
9 let { slug } = useParams();
10 return <div>Now showing post {slug}</div>;
11}
12
13ReactDOM.render(
14 <Router>
15 <Switch>
16 <Route exact path="/">
17 <HomePage />
18 </Route>
19 <Route path="/blog/:slug">
20 <BlogPost />
21 </Route>
22 </Switch>
23 </Router>,
24 node
25);
26