acdlite / redux-router

Redux bindings for React Router – keep your router state inside your Redux store
MIT License
2.3k stars 216 forks source link

Easiest way to log current location on route change? #240

Closed Robinnnnn closed 8 years ago

Robinnnnn commented 8 years ago

Is it possible to log the current window location every time there's a route change?

I know this is possible in react-router using an onUpdate handler. ReduxRouter doesn't come with such a handler. Could I set up some middleware somehow in my store?

mjrussell commented 8 years ago

You could use a history listener instead. Theres an example here for React Router - https://github.com/tcoopman/ga-react-router. Updated for redux-rotuer (untested):

import { createHistory } from 'history';
const history = createHistory()

// Listen for changes to the current location. The
// listener is called once immediately.
const unlisten = history.listen(location => {
  ga('send', location);
});

// Configure reducer to store state at state.router
// You can store it elsewhere by specifying a custom `routerStateSelector`
// in the store enhancer below
const reducer = combineReducers({
  router: routerStateReducer,
  //app: rootReducer, //you can combine all your other reducers under a single namespace like so
});

// Compose reduxReactRouter with other store enhancers
const store = compose(
  applyMiddleware(m1, m2, m3),
  reduxReactRouter({
    routes,
    history
  }),
  devTools()
)(createStore)(reducer);

React.render(<ReduxRoute/>, el)
silouanwright commented 8 years ago

thanks for posting that example, got it working.