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

Scroll to top on route change #204

Closed martinwells closed 8 years ago

martinwells commented 8 years ago

Hi there,

I would like to add a basic hook that does a: window.scrollTo(0, 0); whenever there is a route change. Any ideas where I could place this?

chentsulin commented 8 years ago

Untested, but you may use scroll-behavior npm module to do this:

import createHistory from 'history/lib/createBrowserHistory'
import useScroll from 'scroll-behavior/lib/useScrollToTop'

// Compose reduxReactRouter with other store enhancers
const store = compose(
  applyMiddleware(m1, m2, m3),
  reduxReactRouter({
    routes,
    createHistory: useScroll(createHistory)
  }),
  devTools()
)(createStore)(reducer)
mjrussell commented 8 years ago

@martinwells Not sure if @chentsulin's example worked but you could also write a custom middleware which inspects the action, checks the type against the routerDidChange type from redux-router and then do the window.scroll