Home > Net >  Check what route i am in React Js
Check what route i am in React Js

Time:10-06

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 ?

CodePudding user response:

There is useParam() hook which you can find out a slug, id and etc., value. Then you can use ternary (or not) if or switch statement.

const {pageName} = useParam();
const pageComponent = pageName === 'admin' ? <Admin /> : <Home />;

CodePudding user response:

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

  • Related