Solved: react get current route

The main problem with react getting current route is that it can be slow.


There is no built-in way to get the current route in React. However, you can use the router's context to get the current location, which will give you the current pathname.

const { location } = this.context.router; console.log(location.pathname); // /some/path

name

uselocation() hook

The uselocation() hook is used to determine whether a component should be rendered at the root or not. If the component should not be rendered at the root, it can use this hook to determine where it should be rendered instead.

How to get routes

There are a few ways to get routes in React. One way is to use react-router. Another way is to use react-router-dom.

Related posts:

Leave a Comment