Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

406
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda