Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

451
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda