React-router: How to listen to Route changes in react-router v4 with HashRouter?

Created on 22 Jul 2017  路  2Comments  路  Source: ReactTraining/react-router

First, I'm sorry for asking a question here. I know this is not a place to ask but I couldn't find the proper way to implement it on docs.

i'm using HashRouter instead of BrowserRoute since I'm managing my react app on AWS s3 which is static hosting.

Now I want to integrate Google Analytics into my app and need to detect Route changes. I looked into documents which react-router team provides but couldn't get one. I know there is a way to detect changes using withRouter with BrowserRouter. but I'm using HashRouter.

Can anyone know how to do it with HashRouter?

Most helpful comment

In previous version I was doing like this:

import { hashHistory } from 'react-router'

hashHistory.listen(() => { window.scrollTo(0, 0); });

to move view to the top when useing smooth-scrool

Is there any equivalent in v4?

All 2 comments

The <HashRouter> is essentially the same as the <BrowserRouter>. Aside from how locations map to URIs, the only real API difference is that <HashRouter> locations can't have state.

There are a number of ways to do this, you just have to subscribe to your history object to be informed of location changes. If you want to do this through React, you can use the <OnUpdate> from my rrc package. You can also just use that as reference and write your own.

In previous version I was doing like this:

import { hashHistory } from 'react-router'

hashHistory.listen(() => { window.scrollTo(0, 0); });

to move view to the top when useing smooth-scrool

Is there any equivalent in v4?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nicolashery picture nicolashery  路  3Comments

alexyaseen picture alexyaseen  路  3Comments

sarbbottam picture sarbbottam  路  3Comments

ArthurRougier picture ArthurRougier  路  3Comments

ackvf picture ackvf  路  3Comments