react router get full current path name

If you're using 1.0 or newer, you have the location as a prop in your React components that are matched against a route. So you just type

this.props.location.pathname

to get what you wanted.


You can also try

location.pathname

It may work while other methods don't as it did to me


For React Functional Component

import { useLocation } from 'react-router-dom';

const MyComponent = () => {
  let location = useLocation();
  ...useState

    useEffect(() => {
    console.log(location.pathname);
  }, []);

  return ();

  };

  export default MyComponent;

There are many other options: https://dev.to/finallynero/hooks-introduced-in-react-router-v5-1-7g8


this.props.location.pathname gives only the routing path.

window.location.href gives you the full URL, as suggested here https://stackoverflow.com/a/39823749/7560899