Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

407
Views
React router get currently active segment as param

I have following router configuration

<Router history={history}>
    <Route path="/" component={ReviewWizard}>
        <IndexRoute component={Administrative}/>

        <Route path="Administrative" component=Administrative}>
            <Route path="/Administrative/:itemId" component={AdministrativeItem}/>
        </Route>
        <Route path="Offense" component={Offense}/>
    </Route>
</Router>

I'm trying to get currently active route segment (ie Administrative or Offense).

Is there a way to do something like this? ie route constraints

<Router history={history}>
    <Route path="/:segment" component={ReviewWizard}>
        <IndexRoute component={Administrative}/>

        <Route path="/:segment=Administrative/:itemId" component={Administrative}>
            <Route path="/Administrative/:itemId" component={AdministrativeItem}/>
        </Route>
        <Route path="/:segment=Offense" component={Offense}/>
    </Route>
</Router>

If not, what is best practice to get the current active route segment? I don't like this.context.router.routes[1].path

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

First off I would recommend the following router config, as it seems that it's what you're aiming for:

<Router history={history}>

  <Route path="/" component={ReviewWizard}>

    <!-- whenever we hit '/' we redirect to '/Administrative' -->
    <IndexRedirect path="/Administrative"/>

    <!-- Renders ReviewWizard <- Administrative -->
    <Route path="/Administrative" component={Administrative}>

      <!-- Renders ReviewWizard <- Administrative <- AdministrativeItem -->
      <Route path="/Administrative/:itemId" component={AdministrativeItem}/>

    </Route>

    <!-- Renders ReviewWizard <- Offense -->
    <Route path="/Offense" component={Offense}/>

  </Route>

</Router>

As for detecting the currently active route (or if a route fragment is active), I would recommend using the router.isActive -method. Simply do something like this:

if (router.isActive('/Administrative')) {
  doSomething()
} else if (router.isActive('/Offense')) {
  doSomethingElse()
}

For something more declarative, I recommend just using the location object that react-router injects into each component it manages:

const { location: { pathname } } = this.props
const [ activeSegment ] = pathname.slice(1).split('/')

Hope these help!

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!