0

Here i am having a path like this and getting it from useLocation hook:

function usePageViews() {
  let location = useLocation()

  React.useEffect(
    () => {
      GApageView(location.pathname)
    },
    [location]
  )
}

let path1 = /account/forgotPassword/eyJhbGciOiJIUzUxMiJ9.ir1AeAKLZexUzGhjJvBuzm9jkqZahLAbRwTmOAjB8ZMpj3QLtkpymFwATNb_rP4fu0YgbRT4H9jetszOOSLEsg/ab/en/

in this above value , i need to get only the path route Eg:

Result:

/account/forgotPassword

in someScenarios , the paths will be like this

let path1 = /account/login
let path2 = /

For this i need to get the value as it is.

Is there anyway i can achieve this.Please help me with this. Thanks in advance

2 Answers 2

1

You can use the useLocation hook from react router:

import { useLocation } from 'react-router-dom'
...
const location = useLocation()
console.log('Path:',location.pathname)
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for the reply. I am getting the value like this only. location.pathname will give the entire pathname. In this i want to get rid of that queryString.
Well , when you send the queryString, try adding ?key=string, and you can use path.split('?')[0], to get the pathname only. If you already know that your path is 2 parts only , use split to get what you need.
0

Ideally you need to pass token, country, lang as query params. You can then simply use useRouteMatch

working demo

Component

const Login = props => {
  const match = useRouteMatch();

  return <div>{match.path.split(":")[0]}</div>;
};

Route

<Switch>
          <Route path="/home" exact component={Home} />
          <Route
            path="/account/login/:token/:country/:lang/"
            exact
            component={Login}
          />
        </Switch>

Nav

     <Link
        className={
          "tab " + currentRoute.includes("contact") ? "tab active" : "tab"
        }
        to="/account/login/eyJMiJ9.ir1LtEsg/ab/en/"
      >
        Login
      </Link>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.