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
1In react-router-dom version 6
2useHistory() is replaced by useNavigate() ;
3
4import {useNavigate} from 'react-router-dom';
5const navigate = useNavigate();
6navigate('/home')