Scroll behaviors for use with history
.
Enhance your history object with one of the scroll behaviors in this library to get the desired scroll behavior after transitions.
import createHistory from 'history/lib/createBrowserHistory'
import useScroll from 'scroll-behavior/lib/useStandardScroll'
const history = useScroll(createHistory)()
$ npm install history scroll-behavior
You may also want to install React Router to obtain a complete routing solution for React that works with history
.
useScrollToTop
scrolls to the top of the page after any transition.
This is not fully reliable for POP
transitions.
useSimpleScroll
scrolls to the top of the page on PUSH
and REPLACE
transitions, while allowing the browser to manage scroll position for POP
transitions.
This can give pretty good results with synchronous transitions on browsers like Chrome that don't update the scroll position until after they've notified history
of the location change. It will not work as well when using asynchronous transitions or with browsers like Firefox that update the scroll position before emitting the location change.
useStandardScroll
attempts to imitate native browser scroll behavior by recording updates to the window scroll position, then restoring the previous scroll position upon a POP
transition.
- Support for async transitions is currently very poor. Fixing this will require major breaking API changes in the future.