Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

163
Vistas
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?

8 months ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

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
})
8 months ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos