Stiven Ballshi
Stiven Ballshi

Reputation: 81

Check what route i am in React Js

I want to do a ternery if i navigated in some route then execute a function conditionally if i am in the Route i want to be, but i can't fint a way to do it . I want to use react router dom but if there is any other way of achieveing that please show me ?

Upvotes: 0

Views: 657

Answers (2)

Asad Raza
Asad Raza

Reputation: 39

You can use the useRouteMatch hook from react-router-dom to match the current URL in your React component. Here's how you can use useRouteMatch:

import { useEffect } from 'react';
import { useRouteMatch, Link } from 'react-router-dom';

// Inside your component
const match = useRouteMatch('/your-interesting-route');

if(match) return null;

return (
   <Link to="/your-interesting-route">
       GO TO  your-interesting-route
   <Link/>
)

Upvotes: -1

Dayoolacodes
Dayoolacodes

Reputation: 56

you could use const { pathname } = useLocation(); from react-router-dom the pathname gives you the active Route

Upvotes: 2

Related Questions