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

447
Vistas
In React Router V6, can I still get RouteComponentProps (or access to history and location) in class components?

Background I'm migrating from v4 to v6. Many of my pages are class components, not functional components, and I relied heavily on RouteComponentProps (written with Typescript):

export class UserDashboard extends React.Component<RouteComponentProps> {
  constructor(props){
    super(props);
    this.updateURL = buildURLUpdater(props.history, props.location, ...);
    console.log(props.user);
  }

  onChooseUser = (newUser) => {
    this.setState({
      user: newUser
    }, this.updateURL);
  }

  ...
}

<Switch>
  <Route path="/users">
    <Users focusUser={user} />
  </Route>
</Switch>

Question I know that React Router V6 relies heavily on hooks, but I'm not sure how to get location or history in a class component. Is this still possible?

Why I wrote a handy function that I can run after every setState command that would automatically update the URL and insert a history item without having to navigate. This would be for when I want the current search text to show up in the URL bar as the user types. I used props.location and props.history to do that. So if I can't get route component props anymore, I need a way to grab the history and location object, or whatever its V6 equivalents are, in the constructor of a class.

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