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

412
Visualizações
In React Router v4 is it possible to subscribe for route changes?

I am wondering what would be the best pattern to subscribe for browser history changes with the latest version of react-router. I was reading the current documentation, but it looks like the only option which is mentioned there is by explicitly retrieving props passed by the <Match/> container to the render function or to the component. This solution is also described here:

https://stackoverflow.com/a/41006114/2817257

But what if I would like to get route parameters deeper in the component tree in a clean way?

What I am thinking about is to create a container component that retrieves router from context and subscribes to location changes. However, with the current version 4.0.0-alpha.6 even though the router object is already in the context, it only contains the following methods:

  • blockTransitions
  • createHref
  • replaceWith
  • transitionTo

which are not very promising, because it looks like router is not exposing the history object at all. Maybe there's some other object that's added to the context that could be helpful?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

react-router docs are down ATM, but in v3 you could pass an onUpdate function as a prop to the Router component to listen to router state changes.

Other option is to import browserHistory, which is is implemented with history (again v3 knowledge as v4 docs are down) and subscribe to history changes with listen, like this

import { browserHistory } from 'react-router'

browserHistory.listen((location, action) => {
  // do whatever you need here
})
over 4 years ago · Santiago Trujillo Relatório
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