1/*In the 5.1 release of react-router there is a hook called useLocation,
2which returns the current location object.
3This might be useful any time you need to know the current URL.*/
4import { useLocation } from 'react-router-dom';
5
6const location = useLocation();
7console.log(location.pathname);
8
9
1import {withRouter} from 'react-router-dom';
2
3const SomeComponent = withRouter(props => <MyComponent {...props}/>);
4
5class MyComponent extends React.Component {
6 SomeMethod () {
7 const {pathname} = this.props.location;
8 }
9}
1/*For class based components*/
2import { withRouter } from 'react-router-dom';
3
4class SomeComponent extends Component {
5 render() {
6 console.log(this.props.location)
7 console.log(this.props.location.pathname)
8 console.log(this.props.location.match)
9 return <div>Something</div>
10 }
11}
12
13export default withRouter(SomeComponent)