xxxxxxxxxx
console.log(window.location.pathname); //yields: "/js" (where snippets run)
console.log(window.location.href); //yields: "https://stacksnippets.net/js"
Run code snippet
xxxxxxxxxx
import { useLocation } from 'react-router-dom';
function MyComponent() {
const location = useLocation();
const currentUrl = location.pathname;
console.log(currentUrl); // Prints the current URL to the console
return <div>Your component JSX goes here</div>;
}
xxxxxxxxxx
import {withRouter} from 'react-router-dom';
const SomeComponent = withRouter(props => <MyComponent {props}/>);
class MyComponent extends React.Component {
SomeMethod () {
const {pathname} = this.props.location;
}
}