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

247
Views
React router v6 cómo obtener la ruta actual en el componente de clase

Problema:

Los enlaces de enrutador React se pueden usar en componentes de función para recuperar la ruta actual.

Sin embargo, no puedo encontrar ninguna referencia para el componente de clase .

manera funcional

Caso:

Tengo un menú de navegación y quiero realizar un seguimiento de la ruta actual para resaltarla en la parte delantera cambiando las clases de css según el estado actual.

Extracto de navegación:

 import React, { Component, Fragment } from 'react'; import { Link } from 'react-router-dom'; ... export default class Nav extends Component { ... render = (): JSX.Element => { return ( <> {/* Current: "border-green-500 text-gray-900" */} {/* Default: "border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-700" */} <Link to="/" className="border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-700 inline-flex items-center px-1 pt-1 border-b-2 text-sm font-medium" > Home </Link> </> ); } }

Pregunta:

¿Hay algún método para lograr este objetivo en un componente de clase de reacción?

Respuesta esperada:

  • Referencia a un ejemplo de trabajo
  • Fragmento de código
  • Explicación de por qué esto no funcionaría
  • Solución alternativa

¡Gracias de antemano!


¡Gracias a @genius fox dev, esta solución funcionó para mí!

Solución:

 {/* Current: "border-green-500 text-gray-900" */} {/* Default: "border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-700" */} <NavLink // from 'react-router-dom' to="/" className={ ( {isActive} ) => ( isActive ? 'border-green-500 text-gray-900' : 'border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-700' ).concat( ' inline-flex items-center px-1 pt-1 border-b-2 text-sm font-medium' ) } > Home </NavLink>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede utilizar NavLink compatible con react-router-rom v6.0.

Primero, cambie el enlace a navlink y luego muestre cuidadosamente el código debajo.

 <NavLink style={({ isActive }) => { return { display: "block", margin: "1rem 0", color: isActive ? "red" : "" }; }} to={`/invoices/${invoice.number}`} key={invoice.number} > {invoice.name} </NavLink>

por favor, consulte la URL. https://reactrouter.com/docs/en/v6/getting-started/tutorial#active-links

about 4 years ago · Juan Pablo Isaza 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!