DEV Community

Charles 🛰
Charles 🛰

Posted on

How to know your url location on react (with react-router-dom)

In this post we will be able to see how to know the url we are currently on so for example if we have to fire an event depending on the url we can do it.

First of all we need to have installed or we have to be using react-router-dom

If you already got it let's begin

First import it at the top of your js or ts file

import { useLocation } from 'react-router-dom';
Enter fullscreen mode Exit fullscreen mode

Once you import it we can call it like this inside the component

const Home = () => {
  const location = useLocation();

  useEffect(() => {
    console.log(location.pathname)
}, [])
return(
  <div></div>
)
}
Enter fullscreen mode Exit fullscreen mode

Now if you go to the console on the browser you will be able to see the path you currently at.

Top comments (0)