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

410
Views
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 answers
Answer question

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 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!